我想尝试弄清楚如何获取从Yelp Business API 2.0输出的数据,并实际选择我想要回显的位,添加样式,等等。
以下是基本查询:
<?php
require_once ('lib/OAuth.php');
$unsigned_url = "http://api.yelp.com/v2/business/the-waterboy-sacramento";
// Set your keys here
$consumer_key = 'mykey';
$consumer_secret = 'mysecret';
$token = 'mytoken';
$token_secret = 'mytokensecret';
$token = new OAuthToken($token, $token_secret);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);
$oauthrequest->sign_request($signature_method, $consumer, $token);
$signed_url = $oauthrequest->to_url();
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$response = json_decode($data);
$json_string = file_get_contents($signed_url);
$result = json_decode($json_string);
// Print it for debugging
echo '<pre>';
print_r($result);
echo '</pre>';
?>
在打印数据的区域中,如何修改此项,例如,仅返回 商家名称?或者只是商家地址?或两个项目,每个项目都包含在自己的div中?
是否有一个简单的方法,例如:
echo $business_name;
echo $business_address;
我基本上只是想弄清楚如何大量输出信息,这是基本的文本格式,默认情况下没有任何格式,实际上选择我要显示的内容,给每个位一个div它自己的类,所以我可以设计它并使它漂亮。
目前,print_r
返回:
stdClass Object
(
[is_claimed] =>
[rating] => 4
[mobile_url] => http://m.yelp.com/biz/the-waterboy-sacramento
[rating_img_url] => http://s3-media4.fl.yelpcdn.com/assets/2/www/img/c2f3dd9799a5/ico/stars/v1/stars_4.png
[review_count] => 485
[name] => The Waterboy
[snippet_image_url] => http://s3-media2.fl.yelpcdn.com/photo/yd0M2_X6-S7mg_jFT4KtnQ/ms.jpg
[rating_img_url_small] => http://s3-media4.fl.yelpcdn.com/assets/2/www/img/f62a5be2f902/ico/stars/v1/stars_small_4.png
[url] => http://www.yelp.com/biz/the-waterboy-sacramento
[menu_date_updated] => 1387494198
[reviews] => Array
(
[0] => stdClass Object
(
[rating] => 5
[excerpt] => DEFINITE REPEAT
First time here. Host greeted us, explained restaurant style, etc. Very nice gentleman, and very knowledgable.
Our server was...
[time_created] => 1406526888
[rating_image_url] => http://s3-media1.fl.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png
[rating_image_small_url] => http://s3-media1.fl.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png
[user] => stdClass Object
(
[image_url] => http://s3-media2.fl.yelpcdn.com/photo/yd0M2_X6-S7mg_jFT4KtnQ/ms.jpg
[id] => HRxSv183RfenlVyFWX4gCw
[name] => Randy S.
)
[rating_image_large_url] => http://s3-media3.fl.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png
[id] => OxfuTiiznTQpwaY6zBa7UQ
)
)
[phone] => 9164989891
[snippet_text] => DEFINITE REPEAT
First time here. Host greeted us, explained restaurant style, etc. Very nice gentleman, and very knowledgable.
Our server was...
[image_url] => http://s3-media4.fl.yelpcdn.com/bphoto/Y4kSp64IBG2M4CGrENIegA/ms.jpg
[categories] => Array
(
[0] => Array
(
[0] => French
[1] => french
)
[1] => Array
(
[0] => Italian
[1] => italian
)
)
[display_phone] => +1-916-498-9891
[rating_img_url_large] => http://s3-media2.fl.yelpcdn.com/assets/2/www/img/ccf2b76faa2c/ico/stars/v1/stars_large_4.png
[menu_provider] => single_platform
[id] => the-waterboy-sacramento
[is_closed] =>
[location] => stdClass Object
(
[city] => Sacramento
[display_address] => Array
(
[0] => 2000 Capitol Ave
[1] => Downtown
[2] => Sacramento, CA 95811
)
[geo_accuracy] => 9
[neighborhoods] => Array
(
[0] => Downtown
[1] => Midtown
)
[postal_code] => 95811
[country_code] => US
[address] => Array
(
[0] => 2000 Capitol Ave
)
[coordinate] => stdClass Object
(
[latitude] => 38.573153
[longitude] => -121.481208
)
[state_code] => CA
)
)
答案 0 :(得分:4)
这里有一个PHP对象,其中包含许多您可以自己检索的公共属性(stdClass
是PHP可以创建对象的最简单的类)。对于名称,请尝试:
echo $result->name;
您可以从print_r
输出中看到某些数据项是明智的嵌套。例如,如果您想要城市,则可以检索它:
echo $result->location->city;
现在,某些项目(例如地址)存储在对象中的数组中。要获取地址的第一行,请尝试以下操作:
echo $result->location->display_address[0];
值得阅读有关对象和数组的PHP手册,了解有关它们的作用以及如何访问它们的更多信息。以上只是基础知识,但会让你开始。
如果您想要整个地址,请考虑将display_address
属性包装在implode
中 - 这会将数组的元素与“粘合”字符串(例如逗号)连接在一起。这也是in the manual。
最后,将数据“原始”渲染为HTML时要小心 - 如果此数据(您不自己控制)包含尖括号,则会破坏您的网站。因此,您需要在htmlspecialchars
或htmlentities
中包含上述所有内容,因此:
echo htmlentities($result->name);