我正在尝试使用从instagram到数据库的特定标记保存图片。 到目前为止,除了写入数据库部分外,一切似乎都很好。订阅是好的,我从Instagram的API的网站上确认:在数据库中保存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 [{“ “object_id”:“hashtag”,“time”:1381903733,“subscription_id”:3848897,“data”:{}}] 2013年10月16日,12 :上午12点[{“changed_aspect”:“媒体“,”对象“:”tag“,”object_id“:”hashtag“,”time“:1381903933,”subscription_id“:3848897,”data“:{}}] 2013年10月16日, {“changed_aspect”:“media”,“object”:“tag”,“object_id”:“hashtag”,“time”:1381904061,“subscription_id”:3848897,“data”:{}}] 2013年10月16日,12:25 am [{“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,数据“:{}}] 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}'其中option_variable
='next_min_id'”; *将option_value的值更改为'0'。
我刚刚说我失踪的json_decode()对发布的数据。我正在尝试,但迄今没有积极的结果...! – Shayan
json_decode()给我和activity.log中的一样,但var_dump()是空的。 – Shayan