我正在尝试从某个位置(在本例中为亚特兰大)解析最新的 Twitter 趋势
这是我的代码:
<html>
<head></head>
<body>
<?php
$init = 'http://api.twitter.com/1/trends/2357024.json?count=1&callback=?&exclude=hashtags';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$init);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
foreach ($obj[0]->trends as $trend) {
echo "<li class=\"atlanta\">".$trend->name."</li>";
}?>
</body>
</html>
期望的结果是<li class="atlanta">Whatever The Trend Is</li>
它大约有 30% 的时间工作 - 但另外 70% 的时间我收到此错误:
致命错误:不能在...中使用 stdClass 类型的对象作为数组
经过一番谷歌搜索后,似乎 obj 必须是数组......我找到的唯一答案是将 $obj 行更改为这样的 true :
$obj = json_decode($result, true);
但是,这只是给了我这个错误:
警告:在...中为 foreach() 提供的参数无效
有谁知道如何将我的代码更改为“数组”,以便它可以 100% 的工作?