Google Maps Geocode to include name of premises - php

Using the code below which works well:
function geocode($address){
$return = array();
$address = urlencode($address);
$key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$url = "https://maps.google.com/maps/api/geocode/json?key=$key&address={$address}";
if (!function_exists("\curl_init")){
\load_curl();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT,6000);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
$resp_json = \curl_exec($ch);
curl_close($ch);
$resp = json_decode($resp_json, true);
if($resp['status']!=='OK') return false;
foreach($resp['results'] as $res){
$loc = array(
"zipcode"=>null,
"formatted"=>null
);
foreach($res['address_components'] as $comp){
if(in_array("postal_code", $comp['types']))
$loc['zipcode'] = $comp['short_name'];
}
$loc['formatted'] = $res['formatted_address'];
$loc['lng'] = $res['geometry']['location']['lng'];
$loc['lat'] = $res['geometry']['location']['lat'];
$return[] = $loc;
}
return $resp;
}
However when I search for an address such as "ZOOM DIGITAL PRINT UNIT 36 BINLEY IND EST HOTCHKISS WAY COVENTRY CV3 2RL" the formatted address is brought back as "Starley Court, Hotchkiss Way, Coventry CV3 2RL, UK"
Is there a way to get the business name included in this request or can it be found by another API?

The Geocoding API is used to convert addresses into lat/lng coordinates and vice-versa or to find addresses for a given place ID.
To get business information you should use the Places API. E.g. try searching for "Zoom Digital Print Ltd" in Google's Autocomplete example and you'll get this business name.
Hope this helps!

Related

Firebase database delete specific data by php

How can I remove firebase specific data? I use the
php Kreait\Firebase library.
$fg = $database->getReference('raw_check_out')->orderByChild('reciptno')->equalTo($recipt)->getSnapshot();
$reb = $fg->getValue();
$fg->remove();
but this is not working.
Based on your code example:
$fg = $database
->getReference('raw_check_out')
->orderByChild('reciptno')
->equalTo($recipt)
->getSnapshot();
Here $fg does not hold the reference, but the snapshot.
If you want to remove the reference after you have retrieved the data you need, you need the reference itself:
$fg = $database->getReference('raw_check_out');
$query = $fg->orderByChild('reciptno')->equalTo($recipt);
$reb = $query->getSnapshot()->getValue();
$fg->remove();
This function is for unsubscribing users if you want to remove "user-id-8776" and your structure is like:
public function unsubscribe($uid)
{
$ref = $this->database->getReference()->getChild('users')->getChild($uid)->orderByChild($uid)->getReference();
$path = $ref->getUri()->getPath();
$ref->remove();
}
For any other data is more or less the same, you just have to find the reference of data you want to delete it and then you can remove it.
Check Image
You have another option to resolve this problem. This solution given below:
$url = "https://<projectID>.firebaseio.com/chatList/".<jsonFileName>."json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec($ch);
curl_close($ch);

Google web search api to get number of results with php

for keyword selection (seo)I need to know only the number of results for specific group of keywords. got this code from google https://developers.google.com/web-search/docs/), but no way. PHP version is 5.6
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=relax&userip=MYIP";
$referrer = "http://localhost:8080/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $referrer);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body);
in this case ... no results at all
I tried other ways ... eg.:
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=relax";
$body = file_get_contents($url);
$json = json_decode($body);
$results= $json->responseData->cursor->resultCount;
in this case I got some schizophrenic results (sometimes I got numbers, sometimes I got nothing)... in any case always under the declared limit of 1000.
any suggestion?
Thanks in advance.

How to get results names from Google Maps Api Search in PHP

I have a url like this, that search something in a location:
https://maps.google.com/maps?q=dentist+Austin+Texas&hl=en&mrt=yf=l
I need a list of the first 2 results like (“Dentist Mr.Example1”,”Dentist Ex.2”).
Watching this question: What parameters should I use in a Google Maps URL to go to a lat-lon? , I think the url is correct.
And watching this one: Get latitude and longitude automatically using php, API
I try this two options:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($data);
and also this:
$content= file_get_contents($url);
preg_match_all("|<span class=\"pp-place-title\">(.*?)</span></span>|",$content,$result);
In both case I have no results at all.
Thanks in advance!
I have a url like this, that search something in a location:
https://maps.google.com/maps?q=dentist+Austin+Texas&hl=en&mrt=yf=l
I need a list of the first 2 results like (“Dentist
Mr.Example1”,”Dentist Ex.2”).
This is a Google Maps API Request.
Here you go:
$searchTerm = 'dentist+austin+texas';
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $searchTerm;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$array = json_decode($response, true);
// var_dump($array);
// Output
$array = $array['results'];
foreach($array as $index => $component)
{
echo '#' . $index . ' ' . $component['formatted_address'] . '<br>';
// show only the first 2 items (#0 & #1)
if($index === 1) {
break;
}
}
Some notes:
Your search term is "dentist Austin textas" and that becomes 'dentist+austin+texas' when it's part of an URL.
The searchTerm is attached to the API URL.
This URL is used for the cURL request.
The raw response is $response.
This is turned into an $array by setting the second paramter of json_decode() to true.
You might var_dump() the array to see the keys. Or simply call this URL in a browser: https://maps.googleapis.com/maps/api/geocode/json?address=dentist+austin+texas
For the display part: that's a simply array iteration. You can get rid of the top-level 'results' array by re-assigning the values to $array.
The output is a numbered list of dentists
Referencing: https://developers.google.com/maps/documentation/geocoding/
This is almost perfect, but I would like to get also the phone number and the ranking (stars for reviews).
The initial question was to list the first to results for dentists in austin,tx using the Google Maps API.
This additional requirement changes the API / webservice to use, in order to retrieve rich data. You want more details about the addresses.
The data elements "phone_number" and "rating" are part of the Google Places Webservice (Place Details). You need to add your key to the request URLs (&key=API_KEY) in order to access this service.
https://developers.google.com/places/webservice/details#PlaceDetailsRequests
This is a Place Detail Requests:
1) From the first request you extract the "place_id"s.
2) With subsequent requests you retrieve the details about each place via the "Place Details" webservice.
Example: here i'm using the placeid for the first entry:
https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJ0aPBm9xLW4YRLBdfVhz_2P0&key=AIzaSyCj9yH5x6_5_Om8ebAO2pBlaqJZB-TIViY
New code:
<?php
// Google GeoCode API
$address = 'dentist+austin+texas';
$array = getGoogleGeoCode($address);
$array = $array['results'];
//var_dump($array);
foreach($array as $index => $component)
{
echo '#' . $index . ' ' . $component['formatted_address'] . ', ' ;
// subsequent request for "Place Details"
$details = getGooglePlaceDetails($component['place_id']);
$details = $details['result'];
//var_dump($details);
echo 'Phone: ' . $details['formatted_phone_number']. ', ' ;
// rating contains the place's rating, from 1.0 to 5.0, based on aggregated user reviews.
if(isset($details['rating'])) {
echo 'Rating: ' . $details['rating'];
}
// show only the first two entries
/*if($index === 1) {
break;
}*/
echo '<br>';
}
function getGooglePlaceDetails($placeid)
{
// your google API key
$key = 'AIzaSyCj9yH5x6_5_Om8ebAO2pBlaqJZB-TIViY';
$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $placeid . '&key=' . $key;
return curlRequest($url);
}
function getGoogleGeoCode($address)
{
$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $address;
return curlRequest($url);
}
function curlRequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
Result:
The array element rating is not always present, because it's review based. Just use var_dump($details); to see what is there and pick what you need.
To reduce the list, remove the comments around the break statement.

Get more than 10 results by google search API in php

I am trying to get 10 pages result listed using the following cod below. When i run the URL directly i get a json string but using this in code it does not returns anything. Please tell me where i am doing wrong.
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=CompTIA A+ Complete Study Guide Authorized Courseware site:.edu&start=20";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
print_r($json);
Now i am using the following code but it outputs only four entries of a page. Please tell me where i am doing wrong.
$term = "CompTIA A+ Training Kit Microsoft Press Training Kit";
for($i=0;$i<=90;$i+=10)
{
$term = $val.' site:.edu';
$query = urlencode($term);
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start='.$i;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body,true);
//print_r($json);
foreach($json['responseData']['results'] as $data)
{
echo '<tr><td>'.$i.'</td><td>'.$url.'</td><td>'.$k.'</td><td>'.$val.'</td><td>'.$data['visibleUrl'].'</td><td>'.$data['unescapedUrl'].'</td><td>'.$data['content'].'</td></tr>';
}
}
Just try with urlencode
$query = urlencode('CompTIA A+ Complete Study Guide Authorized Courseware site:.edu');
$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start=20';

Retrieving nation and street address with google maps api v3 reverse geocoding

I need to retrieve the: street address, nation and maybe also region starting from an array of coordinates.
I'm using this function:
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
$data = json_decode($curlData);
$address = $data->results[0]->address_components[1]->long_name.', '.$data->results[0]->address_components[0]->long_name;
$country = $data->results[0]->address_components[5]->long_name;
$nation = $data->results[0]->address_components[6]->long_name;
This script itself is working and I get back results, but I've noticed that values are not in the same place every time, (I think depending by the available result) so I got wrong values. Ho can I fix that?
Here are two examples of my output, for one location:
http://urbanfiles.linkmesrl.com/files/uploads/test_address_1.php
http://urbanfiles.linkmesrl.com/files/uploads/test_address_2.php
Any suggestion to get the right value for my results?
You can loop through the address components and decide the type from the array "types" field.
<?php
foreach($data->results[0]->address_components as $address_component){
if(in_array('country', $address_component->types)){
$country = $address_component->long_name;
continue;
} elseif(in_array('route', $address_component->types)) {
$address = $address_component->long_name;
continue;
}
// etc...
}

Categories