In my site i want to get the latitude and longitude of given pin code or postal code.
If i give 680721 as pin code or postal code then i want to get its corresponding latitude and longitude using php code.
How can i do this?
If this is possible in PHP?
I have php code to get latitude and longitude of given name of place.
$Url='http://maps.googleapis.com/maps/api/geocode/json?address='.$exp_pjctloc[$i].'&sensor=false';
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$search_data = json_decode($output);
$lat = $search_data->results[0]->geometry->location->lat;
$lng = $search_data->results[0]->geometry->location->lng;
But how can i get it by using pin code or postal code?
Below code worked for me
<?php
$zipcode="92604";
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$zipcode."&sensor=false";
$details=file_get_contents($url);
$result = json_decode($details,true);
$lat=$result['results'][0]['geometry']['location']['lat'];
$lng=$result['results'][0]['geometry']['location']['lng'];
echo "Latitude :" .$lat;
echo '<br>';
echo "Longitude :" .$lng;
?>
This worked for me:
$zip = 94043;
$site = file_get_contents('http://geocoder.ca/?postal='.$zip, false, NULL, 1000, 1000);
$goods = strstr($site, 'GPoint('); // cut off the first part up until
$end = strpos($goods, ')'); // the ending parenthesis of the coordinate
$cords = substr($goods, 7, $end - 7); // returns string with only the
$array = explode(', ',$cords); // convert string into array
echo "<pre>";
print_r($array); // output the array to verify
With the curl you can get lat and long from below code:
$zipcode=90012;
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($zipcode)."&sensor=false&key=XXXXXXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$search_data = json_decode($output);
$lat = $search_data->results[0]->geometry->location->lat;
$lng = $search_data->results[0]->geometry->location->lng;
Related
Hello I have a successful cURL request that returns the 8 largest cities in a country including the city name, country code, latitude and longitude ($largeCities['records']). What I then want to do is loop through that array and pass the latitude, longitude and country code of each city to a new cURL routine (geonames findNearbyWikipedia api) and return that data to use in my JavaScript. I'm wondering if this is possible and if so where am I going wrong. Currently when I run the $.ajax request on my php file I'm just getting an empty array cityCluster: Array(0)
relevent php code:
//large cities in country
$url='https://public.opendatasoft.com/api/records/1.0/search/?dataset=geonames-all-cities-with-a-population-1000&q=&rows=8&sort=population&facet=timezone&facet=country&refine.country_code='. $countryCodeA2;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$largeCities = json_decode($result,true);
$output['data']['largeCities'] = $largeCities['records'];
foreach ($largeCities['records'] as $key => $value) {
$cityLat = $value['geometry']['coordinates'][0];
$cityLng = $value['geometry']['coordinates'][1];
$countryCodeISO2 = $value['fields']['country_code'];
//wiki cities clusters
$url='http://api.geonames.org/findNearbyWikipediaJSON?formatted=true&lat=' . $cityLat . '&lng=' . $cityLng . '&country='. $countryCodeISO2 .'&maxRows=15&username=' . $geoUserName . '&style=full';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
$cityCluster = json_decode($result,true);
};
//output status
$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "success";
$output['status']['executedIn'] = intval((microtime(true) - $executionStartTime) * 1000) . " ms";
$output['data']['cityCluster'] = $cityCluster['geonames'];
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($output);
I have a very strange problem. I am trying to return an array from a JSON GET request using the Skyscanner API.
The Skyscanner URL I am generating and using in the cURL is: http://partners.api.skyscanner.net/apiservices/browseroutes/v1.0/DE/EUR/de-DE/HAM/PT/2016-12?apiKey=API_KEY
It returns 3 quotes but when I am using a JSON editor like http://www.jsoneditoronline.org/ it gives me 9 quotes!!!
Why does it show less quotes in my solution? And how do I get all quotes???
<?php
$cities = array("HAM");
$countries = array("PT");
$url_api = "http://partners.api.skyscanner.net/apiservices/browseroutes/v1.0/DE/EUR/de-DE/";
$api_key = "API_KEY_HERE";
$next_month = date('m')+1;
if ($next_month=="13") {
$this_year = date('Y')+1;
$next_month = "01";
} else {
$this_year = date('Y');
}
foreach($cities as $city){
foreach($countries as $country){
$url = $url_api.$city."/".$country."/".$this_year."-".$next_month."?apiKey=".$api_key;
$ip = "218.255.245.210";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'X_FORWARDED_FOR: '.$ip));
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
$data = curl_exec($ch);
$response = json_decode($data);
echo $url."<br/>";
$quotes = $response->Quotes;
foreach($quotes as $quote){
echo $quote->QuoteId.". ";
echo $quote->Direct."<br/>";
}
}
}
?>
When i am Decoding using commented "$jsonString" String it is working very well.
But after using curl it is not working, showing Null.
Please Help Me in this.
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
$url = 'http://ExampleStatus.php?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// $jsonString = '[{"branchname":"BHUBNESHWAR","consignee":"ICICI BANK LTD","currentstatus":"Delivered by : BHUBNESHWAR On - 25/07/2015 01:00","dlyflag":"Y","PODuploaded":"Not Uploaded"}]';
if ($jsonString != '') {
$json = str_replace(array('[', ']'), '', $jsonString);
echo $json;
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}
}
}
After curl I used following concept to get only JSON data from HTML Page.
1) fetchData.php
$url = 'http://DocketStatusApp.aspx?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// now get only value
$dom = new DOMDocument();
$dom->loadHTML($jsonString);
$thediv = $dom->getElementById('Label1');
echo $thediv->textContent;
2) JSONprocess.php
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
ob_start(); // begin collecting output
include_once 'fetchData.php';
$result = ob_get_clean(); // Completed collecting output
// Now it will show & take only JSON Data from Div Tag
$json = str_replace(array('[', ']'), '', $result);
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}
Please I have made this code to get the longitude and the latitude :
$addresss = "France+mande-lieu-la-napol";
$region = "Alpes-Maritimes";
$urll = "http://maps.google.com/maps/api/geocode/json?address=$addresss&sensor=false®ion=$region";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urll);
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($response);
var_dump ($response_a->results[0]->geometry->location->lat );
var_dump ( $response_a->results[0]->geometry->location->lng );
This code gives me as result :
float(43.546232) float(6.938309)
But in real it must give me
float(43.546232) float(6.938309000000004)
Please what should I do to get the full longitude ?
You should use json_decode($json, true, 512, JSON_BIGINT_AS_STRING) so that the lat and long will be returned as strings instead of floats.
if i understand the google error OVER_QUERY_LIMIT right, than it comes because of too many ip-requests.
BUT if I go to the browser and load the following link:
http://maps.googleapis.com/maps/api/geocode/xml?address=Paris&sensor=true
I can see the xml!
So why is that?
Do I have a problem in my code?
Here it is:
$the_map = htmlspecialchars($_POST['map'], ENT_QUOTES);
error_reporting(0);
$map_query = file_get_contents('http://maps.google.com/maps/api/geocode/xml?address='.rawurlencode($the_map).'&sensor=false');
error_reporting(1);
if ($map_query){
$geo_code = simplexml_load_string(utf8_encode($map_query));
if ($geo_code){
$map_lat = $geo_code->xpath("/GeocodeResponse/result/geometry/location/lat");
$map_lng = $geo_code->xpath("/GeocodeResponse/result/geometry/location/lng");
$map_lat = $map_lat[0][0];
$map_lng = $map_lng[0][0];
}
}
Use curl instead of file_get_contents:
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=India";
$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($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;
Or See the below URL
Warning while using file_get_contents for google api