将flickrjson数据插入mysql数据库

py49o6xq  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(380)

我使用地理位置和flickrapi获得了json数据。现在我正在尝试将这些数据插入mysql数据库。我可以将经纬度存储到数据库中。我需要关于flickr json数据的帮助。我不知道如何将其插入数据库。任何帮助都将不胜感激

<?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 "</pre>";

    $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 "<pre>";
    print_r($out);
    echo "</pre>";

        $sql = "INSERT INTO geo (latitude, longitude) VALUES ($lat, $lon)";

        if ($db->query($sql) !== False) {
            echo "New record created successfully";
        } 
        else {
                echo "Record Entry Failed";
            }

        $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;
            $sql = "INSERT INTO flickr (id, owner, secret, server, farm, title) VALUES ($id, $owner, $secret, $server, $farm, $title)";
            if ($db->query($sql) !== false) {
            echo "New record created successfully";
            } 
            else{
                echo "FAILED";
            }
            $url = 'http://farm'.$photo->farm.'.staticflickr.com'.'/'.$photo->server.'/'.$photo->id.'_'.$photo->secret.'_b'.'.jpg';
            echo '<img src="'.$url.'"/><br/>';
            echo "<pre>";
            print_r($id);
            echo "</pre>";

}   

    $conn->close();

}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>geogram</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="script.js"></script>
</head>
<body>
<form action="" method="post">
    <input type="text" name="address"/>
    <button type="submit">Submit</button>
</form>
<br/>

</body>
</html>

数据库表

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对象([photos]=>stdclass对象([page]=>1[page]=>15452[perpage]=>20[total]=>309028[photo]=>array([0]=>stdclass对象([id]=>30662996107[owner]=>9449122@n02 [秘密]=>4010d5b691[服务器]=>1925[农场]=>2[标题]=>白姜-10月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
)
xtfmy6hx

xtfmy6hx1#

试试这个代码。我没有将“search\ id”包含到查询中,因为您的geo表模式表示它是自动递增的。

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";
                                    }
                                } 

                            }

相关问题