我正在尝试使用Google Places API获得一些评论。 JSON可以很好地返回,但是无论我使用哪种方法,我都无法让foreach循环遍历评论。
我尝试使用json_decode,将其设置为对象,数组...不确定我在做什么错,我确定这是我所缺少的简单东西。我需要一双新鲜的眼睛。
以下是使用file_get_contents
而不使用json_decode
的示例响应。
{
"html_attributions":[
],
"result":{
"reviews":[
{
"author_name":"poiny 1965",
"author_url":"https://www.google.com/maps/contrib/106718355093949178742/reviews",
"language":"en",
"profile_photo_url":"https://lh4.googleusercontent.com/-xLgQJ6zvmp8/AAAAAAAAAAI/AAAAAAAAAAA/ACevoQO04SZnES1wQYg78lppVVHiQsyL3w/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating":5,
"relative_time_description":"9 months ago",
"text":"Rick Lisek really knows his fishing!!! We limited out in the first hour of our trip. Amazing trip and so much fun. Best guide in Branson by FAR!",
"time":1524435416
},
{
"author_name":"advantage_ins01",
"author_url":"https://www.google.com/maps/contrib/113907054087036345834/reviews",
"language":"en",
"profile_photo_url":"https://lh5.googleusercontent.com/-SFLlNrrYyVU/AAAAAAAAAAI/AAAAAAAAAAA/SlCw1_Yp1E0/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating":5,
"relative_time_description":"a year ago",
"text":"We had a great experience on our fishing trip with Brandon Guided Fishing Tours! We all caught our limit, enjoyed Ricks company and knowledge. He went out of his way working with us to get out trip booked last minute. Five stars! We wouldn't hesitate to recommend Branson Guided fishing trips and especially Rick to any and all our friends.",
"time":1498171166
},
{
"author_name":"Anthony Dooley",
"author_url":"https://www.google.com/maps/contrib/115423426947600048919/reviews",
"language":"en",
"profile_photo_url":"https://lh4.googleusercontent.com/-SLuXMK30spw/AAAAAAAAAAI/AAAAAAAAAAA/ACevoQPZPMXruClPBixUzfIApM0OR2DI6Q/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating":5,
"relative_time_description":"2 years ago",
"text":"My 10 year old son and I went out with Rick's partner Ed this morning and had a great time. Both of us have very little fishing experience but had a blast. My son caught 8 trout and I caught 2 myself. Ed cleaned the fish for us at the end. He was very patient and made the trip very enjoyable for us despite the rain we had the whole time. Thanks Ed!",
"time":1464234004
},
{
"author_name":"Bennie Gremillion",
"author_url":"https://www.google.com/maps/contrib/108824907367840034202/reviews",
"language":"en",
"profile_photo_url":"https://lh5.googleusercontent.com/-ZmopxEC956g/AAAAAAAAAAI/AAAAAAAAAAA/ACevoQPgxVj2fzLdl_mfSyd17i5-eK5O3g/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating":5,
"relative_time_description":"a year ago",
"text":"My Wife and I fished trout on Lake Taneycomo with Ed Phillips, we had a great time. We caught our limit of trout in a very short time in the morning and released many more. Ed is a very experienced guide with lots of knowledge of the area. Rick was a pleasure to book a fishing trip with I called he replied with details in a message and voilà easy as that! I would recommend to them anyone.",
"time":1499288164
},
{
"author_name":"Charles Hancock",
"author_url":"https://www.google.com/maps/contrib/114897985775150327964/reviews",
"language":"en",
"profile_photo_url":"https://lh3.googleusercontent.com/-ZC86q9-joNI/AAAAAAAAAAI/AAAAAAAAAAA/ACevoQOSfjR70RpQLkjBle_3NavGBvYLWg/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating":5,
"relative_time_description":"2 years ago",
"text":"We have fished with Rick several times now and we always have a great time. He knows his stuff and we always catch fish. Our kids and family always rate this part of our vacations as the best experience. We will definitely be back and fish again.",
"time":1457924322
}
]
},
"status":"OK"
}
作为示例,我尝试了以下操作:
<?php
$results = json_decode(file_get_contents($url));
foreach($results->result as $data) {
foreach($data->reviews as $review) {
echo $review->author_name;
}
}
?>
这不适用于第二个循环。
我也尝试过:
<?php
$results = json_decode(file_get_contents($url));
foreach($results->reviews as $key => $review) {
echo $review->author_name;
}
?>
答案 0 :(得分:1)
您可以通过链接名称来取消对嵌入对象的引用:
$results = json_decode(...);
foreach ($results->result->reviews as $review) {
print_r($review);
}
输出:
stdClass Object
(
[author_name] => Charles Hancock
[author_url] => https://www.google.com/maps/contrib/114897985775150327964/reviews
[language] => en
[profile_photo_url] => https://lh3.googleusercontent.com/-ZC86q9-joNI/AAAAAAAAAAI/AAAAAAAAAAA/ACevoQOSfjR70RpQLkjBle_3NavGBvYLWg/s128-c0x00000000-cc-rp-mo/photo.jpg
[rating] => 5
[relative_time_description] => 2 years ago
[text] => We have fished with Rick several times now and we always have a great time. He knows his stuff and we always catch fish. Our kids and family always rate this part of our vacations as the best experience. We will definitely be back and fish again.
[time] => 1457924322
)
...
答案 1 :(得分:1)
您可以通过单个foreach()
循环尝试这种方式。
$results = json_decode($json);
foreach($results->result->reviews as $key=>$data) {
echo $data->author_name.PHP_EOL;
}
输出:
poiny 1965
advantage_ins01
Anthony Dooley
Bennie Gremillion
Charles Hancock