【发布时间】:2012-10-30 12:52:31
【问题描述】:
我正在为 Instagram 使用这种结构:
<?php
// Supply a user id and an access token
$userid = "--user--";
$accessToken = "--token--";
// 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/--user--/media/recent/?access_token=--token--");
$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 ?>
但是这个印刷品:
警告:在第 40 行的 /home/mysite/index.php 中为 foreach() 提供的参数无效
40.行:
<?php foreach ($result->data as $post): ?>
这个结构有什么问题?
这个结构是http://www.blueprintinteractive.com/blog/how-instagram-api-fancybox-simplified
【问题讨论】:
-
在 foreach 结构之前使用
var_dump($result->data)以了解有关 $result->data 对其无效参数的更多信息。 -
很可能您的 JSON 解码失败并返回
false。你能把$result的文本值放在你的帖子里吗?您是否通过jsonlint 之类的方式运行$result的文本值? -
结构没有问题,很可能
$result->data包含无效数据(即不是数组,或者为空)。