将Instagram API订阅保存在数据库中

时间:2013-10-16 07:27:07

标签: php mysql sql json instagram

我正在尝试将带有特定标记的图片从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”:“media”,“object”:“tag”,“object_id”:“hashtag”,“time”:1381903108,“subscription_id”:3848897, “数据”:{}}]   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”:“media”,“object”:“tag”,“object_id”:“hashtag”,“time”:1381904759,“subscription_id”:3848897,“data” :{}}]   2013年10月16日上午12:26 [{“changed_aspect”:“media”,“object”:“tag”,“object_id”:“hashtag”,“time”:1381904776,“subscription_id”:3848897,“data” :{}}]   2013年10月16日上午12:29 [{“changed_aspect”:“media”,“object”:“tag”,“object_id”:“hashtag”,“time”:1381904973,“subscription_id”:3848897,“data” :{}}]

我创建了一个包含两个表(照片,global_option)的数据库,其中包含所需的列。似乎只对数据库起作用的是*“UPDATE global_option SET option_value ='{$ min_tag_id}'WHERE option_variable ='next_min_id'”; *改变了option_value的值为'0'。

1 个答案:

答案 0 :(得分:0)

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

json_encode($ tagMedia);

foreach((数组) $ tagMedia-&gt;数据为$ entry)