【发布时间】:2012-10-03 06:42:37
【问题描述】:
我正在尝试与 instagram API 连接,连接工作正常,我正在接收 API 文档中描述的更新,问题是我无法访问发送到我的回调函数的数据。
根据doc
当有人发布新照片并触发更新您的订阅时,我们会向您在订阅中定义的回调 URL 发出 POST 请求
这是我的代码:
// check if we have a security challenge
if (isset ($_GET['hub_challenge']))
echo $_GET['hub_challenge'];
else // This is an update
{
// read the content of $_POST
$myString = file_get_contents('php://input');
$answer = json_decode($myString);
// This is not working starting from here
$id = $answer->{'object_id'};
$api = 'https://api.instagram.com/v1/locations/'.$id.'/media/recent?client_secret='.INSTA_CLI_SECRET.'&client_id='.INSTA_CLI_ID;
$response = get_curl($api); //change request path to pull different photos
$images = array();
if($response){
$decode = json_decode($response);
foreach($decode->{'data'} as $item){
// do something with the data here
}
}
}
显示 $myString 变量我有这个结果,不知道为什么它没有解码为 json :(
[{"changed_aspect": "media", "subscription_id": 2468174, "object": “地理”,“object_id”:“1518250”,“时间”:1350044500}]
当我硬编码我的 $id 时,get_curl 函数工作正常。
我猜我的 $myString 有问题,不幸的是 $_POST cvariable 没有填充,知道我缺少什么吗?
【问题讨论】:
-
你应该使用
$answer->object_id代替,而不是引用用{''}包裹的对象,你可以直接访问它,即$obj->method_name。 -
我这样做时出错:尝试获取非对象的属性
-
对象在一个数组中。试试
$answer[0]->object_id。 -
哦,如果不行,加
var_dump($answer)看看这个变量的结构到底是什么。 -
感谢 Arno,这正是解决方案!