无法通过PHP从Yelp API中提取值

时间:2011-09-09 16:06:22

标签: php arrays json api yelp

让我站在前面:我是一个PHP黑客。这里可能有一些愚蠢的错误。如果你看到它们,请指出它们。

我正在尝试做什么:我正在为希望显示Yelp评论的餐厅创建一个页面。我正在使用Yelp Phone API来获取特定业务的评论。请查看位于此处的Yelp API文档的示例响应:http://www.yelp.com/developers/documentation/phone_api#sampleResponse

我做了什么:

  • 已成功连接到API并返回了回复
  • 在foreach循环中回显响应数组中的值。

如果您查看文档,则可以看到响应的几个级别。我可以轻松地打印,回显,来自第二层的任何值,但我真正追求的是全部嵌套在响应的“评论”部分。我无法弄清楚如何回应评论部分中的值(例如user_name,review_excerpt等)。

我的代码:

$yelpstring = file_get_contents("http://api.yelp.com/phone_search?phone=[redactedphonenumber]&ywsid=[redactedapikey]", true);
$obj = json_decode($yelpstring);

foreach($obj->businesses as $key => $business)
{
$reviews = $business->reviews;

//print_r($reviews);
echo $reviews['user_name'];
}

如果我回复$ review,我只会得到“Array”这个词。如果我print_r($ reviews),我会得到一个预期的键和值列表。如果我试图从数组中回显一个特定的值(echo $ reviews ['user_name'],我什么也得不到。任何关于我做错了什么的亮点都会非常感激。我肯定我错过了一些简单的东西谢谢你的时间!

编辑:print_r($ reviews)输出:

Array ( [0] => stdClass Object ( [rating_img_url_small] => http://media4.px.yelpcdn.com/static/201012164278297776/img/ico/stars/stars_small_2.png [user_photo_url_small] => http://media2.px.yelpcdn.com/static/201012162819681786/img/gfx/blank_user_extra_small.gif [rating_img_url] => http://media4.px.yelpcdn.com/static/201012163489049252/img/ico/stars/stars_2.png [rating] => 2 [user_url] => http://www.yelp.com/user_details?userid=vZbcPrYPSMFIDIfTub5H1g [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:u9ckRV6tKApe6Bu93M93CA [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=u9ckRV6tKApe6Bu93M93CA [text_excerpt] => I wanted to like this place. It's got the contemporary name and it's full of hipsters. The place looked clean and the style was fun and cute. I felt like... [user_photo_url] => http://media3.px.yelpcdn.com/static/201012161186834854/img/gfx/blank_user_small.gif [date] => 2011-09-07 [user_name] => boycott p. [id] => u9ckRV6tKApe6Bu93M93CA ) [1] => stdClass Object ( [rating_img_url_small] => http://media4.px.yelpcdn.com/static/201012164278297776/img/ico/stars/stars_small_2.png [user_photo_url_small] => http://media1.px.yelpcdn.com/upthumb/MWu84G5QtmBmT9GoqjT_kg/ss [rating_img_url] => http://media4.px.yelpcdn.com/static/201012163489049252/img/ico/stars/stars_2.png [rating] => 2 [user_url] => http://www.yelp.com/user_details?userid=izF2cGrmqt-u_Z2tDZ8dbg [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:OYLeeCMgnpZkk1c9LWu97g [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=OYLeeCMgnpZkk1c9LWu97g [text_excerpt] => Food is decent and overpriced, but service is a joke. Your food will take a minimum of 20 minutes, for the basic breakfast. Then when your food does come... [user_photo_url] => http://media1.px.yelpcdn.com/upthumb/MWu84G5QtmBmT9GoqjT_kg/ms [date] => 2011-09-06 [user_name] => April H. [id] => OYLeeCMgnpZkk1c9LWu97g ) [2] => stdClass Object ( [rating_img_url_small] => http://media2.px.yelpcdn.com/static/20101216418129184/img/ico/stars/stars_small_4.png [user_photo_url_small] => http://media1.px.yelpcdn.com/upthumb/3euzdGdLZRFxImY68MSg7w/ss [rating_img_url] => http://media2.px.yelpcdn.com/static/201012164084228337/img/ico/stars/stars_4.png [rating] => 4 [user_url] => http://www.yelp.com/user_details?userid=bHR9UU4vtx2QKZD44O0E5g [url] => http://www.yelp.com/biz/jelly-cafe-denver#hrid:njvNAzfSII3PxXyUymLZ1w [mobile_uri] => http://m.yelp.com/biz/5G2X2q9p7QFdm-LbyutltQ?srid=njvNAzfSII3PxXyUymLZ1w [text_excerpt] => Stopped here for breakfast on a friday morning. We were seated immediately and had a really friendly waitress. I ordered a side order of the Chai french... [user_photo_url] => http://media1.px.yelpcdn.com/upthumb/3euzdGdLZRFxImY68MSg7w/ms [date] => 2011-09-05 [user_name] => Diane F. [id] => njvNAzfSII3PxXyUymLZ1w ) ) 

2 个答案:

答案 0 :(得分:1)

$reviews是一系列评论对象。你需要循环它才能获得你想要的数据。

答案 1 :(得分:1)

根据print_r的输出,您无法引用$ reviews ['user_name'];

请注意,$ reviews是一个对象数组。因此,要访问user_name,您需要使用

echo $reviews[0]->user_name;

如果数组中有多个项目,则需要一个像

这样的循环
for ($i = 0; $i<count($reviews); $i++) {
    echo $reviews[$i]->user_name;
}

我希望这会有所帮助。