Converting GPS coordinates captured to address in PHP - php

I want to change real-time coordinates captured from my google maps to address.
I've tried the following code:
<?php
$lat="9.102097";
$long="-40.187988";
geo2address($lat,$long);
function geo2address($lat,$long) {
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&sensor=false";
$curlData=file_get_contents( $url);
$address = json_decode($curlData);
$a=$address->results[0];
return explode(",",$a->formatted_address);
print_r($address);
}
?>
P.S. I'm testing with a fixed coordinates but will capture coordinates from my application when I success
I referenced the code from How to convert GPS coordinates to a full address with php? but it is not working.

There are few issues with the $url itself, such as:
You're using http in the URL, whereas you should use https.
You're not using key parameter in the URL. Sign up for an API KEY and use it in the URL.
Here's an example of one such URL:
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY`
Here's the documentation of reverse geocoding:
https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding
So your code should be like this:
$lat="40.714224";
$long="-73.961452";
geo2address($lat,$long);
function geo2address($lat,$long) {
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&key=YOUR_API_KEY";
$json = json_decode(file_get_contents($url), true);
$a = $json['results'][0]['formatted_address'];
print_r(explode(",",$a));
}
Few points to note here is:
The latitude and longitude are taken from the documentation itself.
Pass the second parameter as true in the json_decode() function to convert it into associative array. It will be then easy for you to access individual components.

Related

How to do bulk lat/long lookup for google geocoding api in PHP?

I am using this endpoint with CURL to get lat/long for specific address and it working fine but if i need to get it for 1000 records then needs to make 1000 api call
is there any way i can supply json of address and get the lat/long for 1000 records in one call
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,
+Mountain+View,+CA&key=YOUR_API_KEY
create foreach loop
$dataLocations = ["London","Slovakia","Ukraine","Kiev","New York"];
foreach($dataLocations as $location){
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$location;
$data = file_get_contents($url);
$json = json_decode($data, true);
return $json;
}

Encoding variables for Google Maps geocoder with PHP

I am trying to encode variables for the URL for the google maps geocoding API with PHP. I have used both urlencode and rawurlencode and I get the same result for
Székesfehérvár, Hungary
PHP returns
Sz%E9kesfeh%E9rv%E1r%2C%20Hungary
Which cannot be decoded by any of the online decoders.
When I encode with the online encoder tools I get:
Sz%C3%A9kesfeh%C3%A9rv%C3%A1r%2C+Hungary
When I plug that in directly to the Google Maps geocoder URL, it is successful. The PHP function fails every time.
This is the code I am using:
function getLatandLong($landmark,$city,$state)
{
global $lat;
global $long;
global $city;
if (!empty($landmark)) { $encoded_vars = "$landmark,$city,+$state";
} else { $encoded_vars = "$city, $state"; }
$encodedVars = urlencode($encoded_vars);
$doc = new DOMDocument();
$doc->load("http://maps.google.com/maps/api/geocode/xml?address=$encodedVars&sensor=false"); //input address
Well, I tried
$encodedVars = utf8_encode($encoded_vars);
And that seems to be working, even though the echo shows (probably because I don;t have the right page encoding set on my catch page:
Székesfehérvár,+Hungary

Get a JSON from url with PHP

Im new to php and tried to get a json object from the twitch API to retrieve one of its values and output it. i.e
i need to get the information from this link: https://api.twitch.tv/kraken/users/USERNAME/follows/channels/CHANNELSNAME
plus i need to to something so i can modify the urls USERNAME and CHANNELSUSERNAME. I want it to be a api to call for howlong user XY is following channelXY and this will be called using nightbots $customapi function.
the date i need from the json is "created_at"
Since we were able to clear out the errorsheres the final PHP file that works if anyone encounters similiar errors:
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
?>
You have a typo in your code on the first line and you're not storing the result of your json_decode anywhere.
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
You have to call the page this way page.php?username=yeroise&channel=ceratia in order to output the created_at value for this user and this channel.
In your code you're using 2 different ways to get the content of the page and you only need one (either file_get_contents or using CURL), I chose file_get_contents here as the other method adds complexity for no reason in this case.

How find Distance between two addresses through php

i am looking for some help to find the distance between two address through php
as i can not use the google maps api. so get a way from this post
Distance between two addresses
but i need to know how can send the request using URL pattern
and grab the repose to save them in database.
http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false
thanks for any suggestion.
///////////////
After these answers i am there
$customer_address_url = urlencode($customer_address);
$merchant_address_url = urlencode($merchant_address);
$map_url = "http://maps.google.com/maps/api/directions/xml?origin=".$merchant_address_url."&destination=".$customer_address_url."&sensor=false";
$response_xml_data = file_get_contents($map_url);
$data = simplexml_load_string($response_xml_data);
XML response is visible when i put this url : http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false
but can not printing through
echo "<pre>"; print_r($data); exit;
You can try this..
$url = "http://maps.google.com/maps/api/directions/xml?origin=550+Madison+Avenue,+New+York,+NY,+United+States&destination=881+7th+Avenue,+New+York,+NY,+United+States&sensor=false";
$data = file_get_contents($url);
Now $data contains resulting xml data. You can parse this xml data using..
http://php.net/manual/simplexml.examples.php
use the haversine formula
Also check https://developers.google.com/maps/documentation/distancematrix/

Currency conversion JSON API parsing with PHP

Could anyone help me parse this JSON API with PHP? I need to retrieve the currency exchange rate.
http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%3D%22eurusd%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=cbfunc
At first you need to omit the last parameter in the URL, just remove &callback=cbfunc.
The PHP code to fetch the content is:
$rawData = file_get_contents("... your url ...");
$parsedData = json_decode($rawData);
$parsedData will now contain the content in a nested object structure.
Further info
You need fopen wrappers enabled for this to work. If they are not enabled, just use cURL to load the content from the page and put it into json_decode.
Here is a function that you can use to convert a currency to another currency using the respective 3 character currency codes (i.e. "USD" to "GBP").
<?php
function convertCurrencyUnit($from_Currency, $to_Currency, $unit_amount = 1) {
$url = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%3D%22' . $from_Currency . $to_Currency . '%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
$rawdata = file_get_contents($url);
$decodedArray = json_decode($rawdata, true);
$converted_unit_amount = $decodedArray['query']['results']['rate']['Rate'];
return $converted_unit_amount * $unit_amount;
}
?>
For example, see the following simple call of this function.
<?php
echo convertCurrencyUnit("USD", "GBP"); //Prints "0.5953" to the browser. The current conversion rate from US Dollar to British Pound as of 04-16-2014.
?>
Also, you can pass an optional third parameter into the function to do a simple multiplication after the conversion is done.

Categories