【发布时间】:2018-01-27 07:37:47
【问题描述】:
前两天我试图通过原始 PHP 显示我的 Instagram 个人资料中的照片。但我遇到了这些错误。
Notice: Trying to get property of non-object in E:\xampp7\htdocs\instagram\index.php on line 29
Warning: Invalid argument supplied for foreach() in E:\xampp7\htdocs\instagram\index.php on line 29
我从 instagram API 创建了正确的身份验证。我请求的 API clients access 是有效的 (clientId, userId, accessToken)。当我使用 PHP 失败时,我尝试使用 JavaScript 插件 instafeedjs。现在它运作良好。这是我的 PHP 代码,如下所示:-
<body>
<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/3550421370/media/recent/?access_token=MY_VALID_ACCESS_TOKEN_IS_HERE");
$result = json_decode($result);
foreach ($result->data as $post) {
if(empty($post->caption->text)) {
// Do Nothing
}
else {
echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
<img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="100%" height="auto" />
<div class="instagram-desc">'.htmlentities($post->caption->text).' | '.htmlentities(date("F j, Y, g:i a", $post->caption->created_time)).'</div></a>';
}
}
?>
</body>
【问题讨论】:
-
在
json_encode()之后和之前使用var_dump($result)。$result->data好像没有定义。 -
似乎 fetchData 没有返回任何内容。
-
是 $eesult->data;是空的
-
我得到了解决方案。我将 Instagram API 链接更改为装扮最近的帖子。作为一个多维数组。可以显示任何关于帖子评论、帖子数量等的信息。 github.com/bravotanmoy/instagram-photo-and-php/blob/master/…
标签: php instagram instagram-api