Вставить данные Flickr JSON в базу данных MySQL - PullRequest
0 голосов
/ 29 октября 2018

Я получил данные JSON, используя Geolocation и Flickr API. Сейчас я пытаюсь вставить эти данные в базу данных MYSQL. Я могу хранить широту и долготу в базе данных. Мне нужна помощь с данными Flickr JSON. Я не уверен, как мне вставить его в базу данных. Любая помощь будет оценена

<code>    <?php
require 'database.php';
if ($_POST['address'])
{   
    $address = $_POST['address'];
    $string = str_replace (" ", "+", urlencode($address));
    $geocode=file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$string.'&key=AIzaSyD5Dqe3SK3kKOZqKT5Rxk_JZskcoCf4J3g');

    $output=json_decode($geocode, true);
    $lat = $output['results'][0]['geometry']['location']['lat'];
    $lon = $output['results'][0]['geometry']['location']['lng'];
    echo "<pre>";
    print_r($output);
    echo "
"; $ insta = file_get_contents ('https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=eabad3d02e824ce76245158db270c855&lat='.$lat.'&lon='.$lon.'&per_page=20&format=json&nojsoncallback=1'); $ out = json_decode ($ insta); echo "
";
    print_r($out);
    echo "
"; $ sql = "INSERT INTO geo (широта, долгота) VALUES ($ lat, $ lon)"; if ($ db-> query ($ sql)! == False) { echo "Новая запись создана успешно"; } еще { эхо "Ошибка записи записи"; } $ photos = $ out-> photos-> photo; foreach ($ photos как $ photo) { $ id = $ photo-> id; $ owner = $ photo-> owner; $ secret = $ photo-> secret; $ server = $ photo-> server; $ farm = $ photo-> farm; $ title = $ photo-> title; $ sql = "INSERT INTO flickr (идентификатор, владелец, секрет, сервер, ферма, заголовок) ЗНАЧЕНИЯ ($ идентификатор, $ владелец, $ секрет, $ сервер, $ ферма, $ title)"; if ($ db-> query ($ sql)! == false) { echo "Новая запись создана успешно"; } еще { эхо "FAILED"; } $ url = 'http://farm'.$photo -> ферма.'. staticflickr.com '.' / '. $ photo-> server.' / '. $ photo-> id .'_'. $ photo-> secret .'_ b ». 'JPG'; эхо 'image
'; echo "
";
            print_r($id);
            echo "
"; } $conn->close(); } ?> <!DOCTYPE html> geogram

Таблицы базы данных

CREATE TABLE `geo` (
  `search_id` int(11) NOT NULL AUTO_INCREMENT,
  `latitude` Decimal(20,15) DEFAULT NULL,
  `longitude` Decimal(20,15) DEFAULT NULL,
  PRIMARY KEY (`search_id`)
) ENGINE=InnoDB ;

CREATE TABLE `flickr` (
  `photo_id` int(20) NOT NULL AUTO_INCREMENT,
  `owner` varchar(256) DEFAULT NULL,
  `filename` varchar(256) DEFAULT NULL,
  `search_id` int(11) DEFAULT NULL,
  `secret` varchar(256) DEFAULT NULL,
  `server` int(11) DEFAULT NULL,
  `farm` int(11) DEFAULT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `title` varchar(256) DEFAULT NULL,
  PRIMARY KEY (`photo_id`),
  KEY `search_id` (`search_id`),
  CONSTRAINT `search_id` FOREIGN KEY (`search_id`) REFERENCES `geo` (`search_id`)
) ENGINE=InnoDB AUTO_INCREMENT=62 DEFAULT CHARSET=latin1;

Разобранные данные JSON: stdClass Object ( [photos] => Объект stdClass ( [page] => 1 [pages] => 15452 [perpage] => 20 [всего] => 309028 [photo] => Массив ( [0] => stdClass Object ( [id] => 30662996107 [владелец] => 9449122 @ N02 [secret] => 4010d5b691 [сервер] => 1925 [ферма] => 2 [title] => Белый имбирь - 27 октября 2018 г. [ispublic] => 1 [isfriend] => 0 [isfamily] => 0 )

                    [1] => stdClass Object
                        (
                            [id] => 31731641758
                            [owner] => 9449122@N02
                            [secret] => 3fa8ec438f
                            [server] => 1915
                            [farm] => 2
                            [title] => White Ginger - October 27, 2018
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [2] => stdClass Object
                        (
                            [id] => 43784782480
                            [owner] => 24243188@N04
                            [secret] => bb4659987b
                            [server] => 1975
                            [farm] => 2
                            [title] => DSC_0480
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [3] => stdClass Object
                        (
                            [id] => 45602624641
                            [owner] => 24243188@N04
                            [secret] => d8ea61ec54
                            [server] => 1913
                            [farm] => 2
                            [title] => DSC_0481
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [4] => stdClass Object
                        (
                            [id] => 43784781020
                            [owner] => 24243188@N04
                            [secret] => 22e8a21890
                            [server] => 1950
                            [farm] => 2
                            [title] => DSC_0482
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [5] => stdClass Object
                        (
                            [id] => 45551846532
                            [owner] => 21151100@N08
                            [secret] => 87ccaabf1f
                            [server] => 1978
                            [farm] => 2
                            [title] => IMG_2871
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [6] => stdClass Object
                        (
                            [id] => 45551808352
                            [owner] => 21151100@N08
                            [secret] => f6e43efb0c
                            [server] => 1966
                            [farm] => 2
                            [title] => IMG_2897
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [7] => stdClass Object
                        (
                            [id] => 30661366227
                            [owner] => 21151100@N08
                            [secret] => 2a666245ca
                            [server] => 1956
                            [farm] => 2
                            [title] => IMG_2899
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [8] => stdClass Object
                        (
                            [id] => 44877542714
                            [owner] => 21151100@N08
                            [secret] => 40341bae71
                            [server] => 1961
                            [farm] => 2
                            [title] => IMG_2900
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [9] => stdClass Object
                        (
                            [id] => 31729843048
                            [owner] => 21151100@N08
                            [secret] => 8bdc59bec0
                            [server] => 1934
                            [farm] => 2
                            [title] => IMG_3015
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [10] => stdClass Object
                        (
                            [id] => 45551677842
                            [owner] => 21151100@N08
                            [secret] => ee7fb953fa
                            [server] => 1973
                            [farm] => 2
                            [title] => IMG_3016
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [11] => stdClass Object
                        (
                            [id] => 30661216397
                            [owner] => 21151100@N08
                            [secret] => e848122232
                            [server] => 1960
                            [farm] => 2
                            [title] => IMG_3018
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [12] => stdClass Object
                        (
                            [id] => 45551663032
                            [owner] => 21151100@N08
                            [secret] => 1b8cd9101e
                            [server] => 1974
                            [farm] => 2
                            [title] => IMG_3019
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [13] => stdClass Object
                        (
                            [id] => 43775021470
                            [owner] => 12203537@N08
                            [secret] => d52463646f
                            [server] => 1945
                            [farm] => 2
                            [title] => 
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [14] => stdClass Object
                        (
                            [id] => 45592785101
                            [owner] => 12203537@N08
                            [secret] => 18d9bdb5ec
                            [server] => 1979
                            [farm] => 2
                            [title] => 
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [15] => stdClass Object
                        (
                            [id] => 45592771261
                            [owner] => 12203537@N08
                            [secret] => 3beffc8e5c
                            [server] => 1962
                            [farm] => 2
                            [title] => Halloween at Mary’s
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [16] => stdClass Object
                        (
                            [id] => 43768485760
                            [owner] => 54059915@N07
                            [secret] => 5a888480cd
                            [server] => 1913
                            [farm] => 2
                            [title] => Crystal Cathedral bell tower
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [17] => stdClass Object
                        (
                            [id] => 30644568537
                            [owner] => 54059915@N07
                            [secret] => e759c4ef84
                            [server] => 1931
                            [farm] => 2
                            [title] => External staircase on Richard Neutra's "Tower of Hope"
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [18] => stdClass Object
                        (
                            [id] => 44860933144
                            [owner] => 54059915@N07
                            [secret] => 9cf81232e6
                            [server] => 1950
                            [farm] => 2
                            [title] => Richard Neutra arboretum in Garden Grove, CA
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                    [19] => stdClass Object
                        (
                            [id] => 45535214712
                            [owner] => 54059915@N07
                            [secret] => 0423fca595
                            [server] => 1914
                            [farm] => 2
                            [title] => Richer Neutra's "Tower of Hope"
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

        )

    [stat] => ok
)

1 Ответ

0 голосов
/ 29 октября 2018

Попробуйте этот код. я не включаю "search_id" в запрос, потому что ваша схема геотаблицы говорит, что это автоматическое увеличение.

                                if ($_REQUEST['address'])
                            {   
                                $address = $_REQUEST['address'];
                                $string = str_replace (" ", "+", urlencode($address));
                                $geocode=file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address='.$string.'&key=AIzaSyD5Dqe3SK3kKOZqKT5Rxk_JZskcoCf4J3g');

                                $output=json_decode($geocode, true);
                                $lat = $output['results'][0]['geometry']['location']['lat'];
                                $lon = $output['results'][0]['geometry']['location']['lng'];

                                $insta = file_get_contents('https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=eabad3d02e824ce76245158db270c855&lat='.$lat.'&lon='.$lon.'&per_page=20&format=json&nojsoncallback=1');
                                $out = json_decode($insta);

                                $sql = "insert into geo (latitude,longitude) values(?,?)";
                                $stmt = $mysqli->prepare($sql);
                                $stmt->bind_param('ss',$lat,$long);

                                foreach($output['results'][0]['geometry']['bounds']  as $row) {
                                    $lat = $row['lat'];
                                    $long = $row['lng'];
                                    $stmt->execute();
                                }
                                $stmt->close();

                                $sql = "insert into flickr (photo_id, owner, secret, server, farm, title) values(?,?,?,?,?,?)";
                                $stmt = $mysqli->prepare($sql);
                                $stmt->bind_param('ssssss',$id,$owner,$secret,$server,$farm,$title);

                                $photos = $out->photos->photo;
                                foreach($photos as $photo) {
                                    $id= $photo->id;
                                    $owner = $photo->owner;
                                    $secret = $photo->secret;
                                    $server = $photo->server;
                                    $farm = $photo->farm;
                                    $title= $photo->title;
                                    $rc = $stmt->execute();
                                    if ( false===$rc ) {
                                      die('execute() failed: ' . htmlspecialchars($stmt->error));
                                    }
                                    $tid = $stmt->insert_id;

                                    if ($tid) {
                                        echo "New record created successfully";
                                    } 
                                    else {
                                        echo "Record Entry Failed";
                                    }
                                } 

                            }
...