我试图使用PHP循环json响应,但是我收到了这个错误:
注意:尝试在第45行获取非对象的属性
这是一行:echo $my_value->website;
这是我的完整代码:
$place = file_get_contents('https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJjSse9T9644kRo9eQsaidHMQ&key=[my key]');
$place_details = json_decode($place);
echo'<table id="table2" class="display" cellspacing="0" width="100%">';
echo'<thead>';
echo '<tr>';`
echo '<th>Website</th>';
echo '</tr>';
echo '</thead>';`
echo '<tbody>';
foreach ($place_details->result as $my_value)
{
echo '<tr>';
echo '<td>';
echo $my_value->website;
echo '</td>';
}
echo '</tbody>';
echo '</table>';
提前致谢
答案 0 :(得分:0)
每website
只有一个 result
,基于此,您可以使用:
$place = file_get_contents('https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJjSse9T9644kRo9eQsaidHMQ&key=AIzaSyDLBcG7vbaj4HmsE6gwSnNDCD0yHUNb9y4');
$place_details = json_decode($place, true);
echo $place_details['result']['website'];
# https://www.bmc.org/