1

i want to get the address from latitude and longitude but i want to do using curl. Without curl is working in localhost fine. But when i run in server it.

showing error this error :-

file_get_contents() [function.file-get-contents]: https:// wrapper is disabled in the server configuration by allow_url_fopen=0.

So decided,, now i have to get the address using curl .

Can anyone help me how to acheive this functionality Here i have tried :-

$latitude= 30.9003971459;
$longitude= 77.0072737194;
$url = 'https://maps.google.com/maps/api/geocode/json?latlng='.trim($latitude).','.trim($longitude).'&key=XXXXX';
$curl = curl_init();
Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
kunal
  • 4,122
  • 12
  • 40
  • 75
  • 1
    Possible duplicate of [Server configuration by allow\_url\_fopen=0 in](https://stackoverflow.com/questions/21677579/server-configuration-by-allow-url-fopen-0-in) – hyubs Aug 01 '17 at 07:42
  • but my question is different please see before duplicating it – kunal Aug 01 '17 at 07:43

1 Answers1

1

Seems tu work form me... after executing the url (CURL_EXEC) you need to decode the JSON return value. I just picked the formatted address in this sample

<?php
$url="https://maps.google.com/maps/api/geocode/json?latlng=51.451411,3.566113&key=AIzaSyAgo83mZXhQCFzF2Y3pQYJUC1ivAXKwiX4";
$curl_return=curl_get($url);

$obj=json_decode($curl_return);

echo $obj->results[0]->formatted_address;

function curl_get($url,  array $options = array())
{
    $defaults = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_TIMEOUT => 4
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}
?>