Reputation: 11
How am i suppose to store the Hotel name and location from the response provided by google place api in PHP array. If my search string is restaurant in New York.
The response of the api is in json format.
My script for sending the search request is:
<?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>';
}
}
?>
Well, the output of the above code is something in json format. But i need to retrieve the Name of the Restaurant and their location from the json output.
So, Please suggest me a way to extract the exact output.
Upvotes: 1
Views: 3671
Reputation: 551
As I can see the results key contains arrays with the data you need. Just do something like:
$jsonContent = json_decode($res, true);
foreach ($jsonContent['results'] as $result) {
// now you have the $result array that contains the location of the place
// and the name ($result['formatted_address'], $result['name']) and other data.
}
I hope you know what to do from this point.
Upvotes: 2