当我解析trip advisor提要时,我一直收到这个错误。试着调出用户名。
可捕获的致命错误:无法将类stdClass的对象转换为字符串
下面是我的代码,其中包含指向提要的链接。
<?php
function getCode($url)
{
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$html=getCode("http://api.tripadvisor.com/api/partner/1.0/location/258705/reviews");
$json = json_decode($html);
$cnt=0;
foreach ($json as $item)
{
foreach($item as $row)
{
echo "Image Url:".$row->text."<br>";
echo "ID:".$row->id."<br>";
//username
foreach($row->user as $row1)
{
echo $row1."<br>";
}
}
echo "<br><br>";
}?>
发布于 2014-05-26 02:54:19
print_r($row1);
stdClass Object ( [id] => [name] => Mahwah )试着改变
$row1."<br>";至
$row1->name."<br>";发布于 2014-05-26 02:51:39
您需要在json_decode函数上将assoc属性设置为true。
试试这个:
$json = json_decode($html,true);阅读有关json_decode here的更多信息
另外,作为下一次堆栈溢出帖子的建议,不要共享API密钥或其他敏感信息。
https://stackoverflow.com/questions/23858735
复制相似问题