我試圖解析從一個位置的最新Twitter的趨勢(亞特蘭大在這種情況下)Twitter的API局部變化趨勢解析 - stdClass的錯誤
這裏是我的代碼:
<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的對象,如數組。 ..
經過一番google搜索似乎OBJ必須是數組...我發現的唯一的答案是在$ OBJ線更改爲真這樣的:
$obj = json_decode($result, true);
然而,簡單地給我這個錯誤:
Warning: Invalid argument supplied for foreach() in...
有誰知道如何改變我的代碼爲「數組」,使其將工作時間的100%?
你需要使用json_decode – ajreal 2010-12-13 20:31:33
請看看我如何格式化編輯解析之前在錯誤處理程序添加到捕捉任何無法預料的錯誤(如網絡倒在你的服務器嘰嘰喳喳) SO上的帖子。這會傷害我的感覺,因爲你沒有花費精力閱讀精心編寫的常見問題解答,這些問題顯示在你的問題框旁邊。 – 2011-07-17 00:20:42