【发布时间】:2018-05-01 18:44:36
【问题描述】:
我想在我的网站上展示 Instagram 照片,使用 PHP7。 我已经注册了我的应用程序,并从 Instagram 添加了 clientId 和 secretId。但是下面的代码给了我错误:data is aof non object and foreach is an invalid argument.
<?php
// Supply a user id and an access token
$clientid = "my client id";
$accessToken = "my client secret";
// Gets our data
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;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$clientid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
?>
<?php foreach ($result->data as $post): ?>
<!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?>
【问题讨论】:
-
var 转储 $result。这是什么?
-
vardump 返回 null
-
你应该考虑使用这个包吗? github.com/vinkla/instagram
-
所以请求失败。看看 curl_error() 给你什么
-
卷曲错误什么也没给我