1

我正在尝试将带有特定标签的图片从 instagram 保存到数据库。到目前为止,除了写入数据库部分之外,一切似乎都很好。订阅没问题,我从 instagram API 网站得到了确认:

https://api.instagram.com/v1/subscriptions?client_secret=XXXXX&client_id=XXXXXX

{"meta":{"code":200},"data":   [{"object":"tag","object_id":"hashtag","aspect":"media","callback_url":"http:\/\/MYDOMAIN.com\/instatag\/instatag\/callback.php","type":"subscription","id":"3848897"}]}

我使用本网站中的教程获取我拥有的代码:Instagram real time photo update with php

更改了一些部分以反映我的设置,但即使我可以看到请求进入,它也没有向数据库写入任何内容。

这是我的代码:

    <?php

require 'instagram.class.php';
require 'appdb.php';

// Initialize class for public requests
// EDIT YOUR CODES HERE
$instagram = new Instagram('hashtag');

$myString = file_get_contents('php://input');
$ALL = date("F j, Y, g:i a")." ".$myString."\r\n";
file_put_contents('./activity.log', $ALL, FILE_APPEND);

// this is how I get my next_min_id and this is just an example
// you can code your own script on how to get next_min_id
// EDIT YOUR CODES HERE
$min_id_result = mysql_query("SELECT * FROM `global_option` WHERE `option_variable`='next_min_id'");
//file_put_contents('./activity.log', (string)$min_id_result, FILE_APPEND);

while ($option_row = mysql_fetch_array($min_id_result)) {
    $next_min_id = $option_row['option_value'];

}

// Get hashtag media
$tagMedia = $instagram->getTagMedia('HASHTAG', $next_min_id);

// Display results
// EDIT YOUR CODES HERE
$rows = array();
//var_dump($tagMedia->data)

foreach ((array)$tagMedia->data as $entry) {
    file_put_contents('./activity.log', $tagMedia->data , FILE_APPEND);

    if (empty($entry)) {

        $url = $entry->images->standard_resolution->url;
        $m_id = $entry->id;
        $c_time = $entry->created_time;
        $user = $entry->user->username;
        $filter = $entry->filter;
        $comments = $entry->comments->count;
        $caption = mysql_real_escape_string($entry->caption->text);
        $link = $entry->link;
        $low_res=$entry->images->low_resolution->url;
        $thumb=$entry->images->thumbnail->url;
        $lat = $entry->location->latitude;
        $long = $entry->location->longitude;
        $loc_id = $entry->location->id;
        $media_id = $entry->id;

        // this is how I check if the media_id is already existed in my table
        $result = mysql_query("SELECT * FROM `photos` WHERE `media_id`='$media_id'");

        // if result is 0, I am inserting new row
        if (mysql_num_rows($result)==0) {
          $sql = "INSERT INTO `photos` (source, media_id, std_res, thumbnail, caption, user_id, user_username, user_full_name, user_profile_pic, published) 
                    VALUES ('Instagram',  '{$entry->id}',  '{$entry->images->standard_resolution->url}', '{$entry->images->thumbnail->url}', '{$caption}', '{$entry->user->id}', '{$entry->user->username}', '{$entry->user->full_name}', '{$entry->user->profile_picture}', '1' )";
            mysql_query($sql);
            file_put_contents('./activity.log', $sql , FILE_APPEND);
            if (!mysql_query($sql)){
              die('Error: ' . mysql_error());
            }
        } else {

            //echo 'exist ' . $entry->id;
        }
    }
}
// SETTING THE next_min_id IN MY TABLE
$min_tag_id = $tagMedia->pagination->min_tag_id;


$sql2 = "UPDATE `global_option` SET `option_value`='{$min_tag_id}' WHERE `option_variable`='next_min_id'";
if (!mysql_query($sql2)){
    die('Error: ' . mysql_error());
}

//$challenge = $_GET["hub_challenge"];
//echo $challenge;
//exit(1);
?>

在 activity.log 我有:

2013 年 10 月 15 日,晚上 11:58 [{“changed_aspect”:“媒体”,“object”:“tag”,“object_id”:“hashtag”,“time”:1381903108,“subscription_id”:3848897,“data” : {}}] 2013 年 10 月 16 日上午 12:08 [{"changed_aspect": "media", "object": "tag", "object_id": "hashtag", "time": 1381903733, "subscription_id": 3848897, "data": {}}] 2013 年 10 月 16 日上午 12:12 [{"changed_aspect": "media", "object": "tag", "object_id": "hashtag", "time": 1381903933 , "subscription_id": 3848897, "data": {}}] 2013 年 10 月 16 日上午 12:14 [{"changed_aspect": "media", "object": "tag”,“object_id”:“hashtag”,“time”:1381904061,“subscription_id”:3848897,“data”:{}}] 2013 年 10 月 16 日,上午 12:25 [{“changed_aspect”:“媒体”, “object”:“tag”,“object_id”:“hashtag”,“time”:1381904759,“subscription_id”:3848897,“data”:{}}] 2013 年 10 月 16 日,上午 12:26 [{“changed_aspect” :“媒体”,“对象”:“标签”,“object_id”:“hashtag”,“time”:1381904776,“subscription_id”:3848897,“data”:{}}] 2013 年 10 月 16 日,上午 12:29 [{“changed_aspect”:“媒体”,“object”:“tag”,“object_id”:“hashtag”,“time”:1381904973,“订阅ID”:3848897,“数据”:{}}]

我创建了一个包含两个表(照片、global_option)的数据库,其中包含所需的列。唯一似乎在数据库上工作的是 *"UPDATE global_optionSET option_value='{$min_tag_id}' WHERE option_variable='next_min_id'";* 将 option_value 的值更改为'0'。

4

1 回答 1

0

好的,现在这解决了问题:

json_encode($tagMedia);

foreach ( (array) $tagMedia->data as $entry)

于 2013-10-17T00:13:28.793 回答