我想如何将 google place api 提供的响应中的酒店名称和位置存储在 PHP 数组中。如果我的搜索字符串是restaurant in New York。
api 的响应是json格式。
我发送搜索请求的脚本是:
<?php
if(isset($_GET['text']))
{
if(!empty($_GET['text']))
{
$search_text=strip_tags($_GET['text']);
$hostname = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=$search_text&sensor=true&key=AIzaSyDVdJtIAvhmHE7e2zoxA_Y9qWRpp6eE2o8";
// read the post from PayPal system and add 'cmd'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$hostname");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$res = curl_exec($ch);
curl_close($ch);
if(!$res)
{
// error log
echo'Unable to find results.';
} else {
**// ALL output printing code goes down here**
//$res=json_decode($res);
//var_dump(json_decode($res));
$obj=var_dump(json_decode($res, true));
print_r($obj['"results']);
}
} else {
echo'<h4><font color="red">Please enter the text to search.</font></h4>';
}
}
?>
嗯,上面代码的输出是json格式的东西。但我需要从 json 输出中检索餐厅的名称及其位置。
所以,请建议我一种提取确切输出的方法。