Php Twitter API本地趋势分析-stdClass错误
我试图从一个位置解析Twitter的最新趋势(本例中为亚特兰大) 这是我的密码:Php Twitter API本地趋势分析-stdClass错误,php,arrays,parsing,twitter,stdclass,Php,Arrays,Parsing,Twitter,Stdclass,我试图从一个位置解析Twitter的最新趋势(本例中为亚特兰大) 这是我的密码: <html> <head></head> <body> <?php $init = 'http://api.twitter.com/1/trends/2357024.json?count=1&callback=?&exclude=hashtags'; $ch = curl_init(); curl_s
<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>
然而,这只是给了我一个错误:
警告:为中的foreach()提供的参数无效
有人知道如何将我的代码更改为“数组”以便它能100%工作吗?@Ken:试试看
$obj = json_decode($result, true);
if (is_array($obj)) {
foreach ($obj[0]->trends as $trend) {
echo "<li class=\"atlanta\">" . $trend->name . "</li>";
}
} else {
// failure case here
}
$obj=json\u decode($result,true);
if(is_数组($obj)){
foreach($obj[0]->趋势为$trend){
echo“”$trend->name.“ ”;
}
}否则{
//这里的失败案例
}
在使用json解析之前,您需要添加错误处理程序以捕获任何未预料到的错误(如服务器到twitter的网络故障)。请查看我的编辑,了解如何格式化SO上的帖子。你没有费心去阅读精心编写的FAQ,这让我很伤心。FAQ是在你写问题的方框旁边的显眼位置展示给你的。谢谢。。。我尝试了该修复,但仍然出现以下错误:警告:为foreach()提供的参数无效。。。。。
$obj = json_decode($result, true);
if (is_array($obj)) {
foreach ($obj[0]->trends as $trend) {
echo "<li class=\"atlanta\">" . $trend->name . "</li>";
}
} else {
// failure case here
}