3

This question is similar to another one answered, however the solution in that case was to use a country code, which is not feasible for this particular use, as the address is being provided by the user through an input field (so a country may not be provided).

Here is the content of my current request

Request coming from AngularJS:

function getCoordinatesFromApi(address) {
            addressApiResponse = $http({
                url: 'php/coordinates.php',
                method: 'get',
                params: {
                    address: address
                },
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            });
            return addressApiResponse;
        }

Request handled in PHP:

$api_maps = 'https://maps.googleapis.com/maps/';
$address = urencode($_GET['address']);
$api_key = 'API_key_here';
$url = $api_maps . 'api/geocode/json?address=' . $address . '&key=' . $api_key;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curl_response = curl_exec($curl);
curl_close($curl);
echo ($curl_response);

When this is called, I consistently get back a valid response with status: 200, but data is an empty string.

I've checked the validity of the $url being build in PHP and it is ok, accessing that url directly in the browser displays a valid API response with data.

I've also tried using Angular's $http method and that too returns a valid response from the API:

function getCoordinatesFromApi(address) {
            addressApiResponse = $http.get('https://maps.googleapis.com/maps/api/geocode/json?address=' + address + '&key=API_key_here');
            return addressApiResponse;
        }

For some reason, it's ony the curl method that does not behave as expected. Has any one dealt with this problem?

AT82
  • 71,416
  • 24
  • 140
  • 167
Alexandra
  • 137
  • 7

2 Answers2

0

If you are saying that echo ($curl_response); return nothing even if status is 200, it's because it's encoded JSON!

Please try this :

$decodedresponse = json_decode($curl_response);

//send me what var_dump return so I can hellp you accessing the array!
echo var_dump($decodedresponse);


//You would use as an array DEPENDING ON HOW THE ARRAY IS BUILD OF COURSE
echo $decodedresponse['response']['lat'];
echo $decodedresponse['response']['long'];
btc4cash
  • 375
  • 1
  • 4
  • 15
  • I've tried this now but it doesn't seem to work either. With your changes in place, data now returns 'NULL' and nothing else. – Alexandra Aug 18 '17 at 09:01
  • $decodedresponse = json_decode($curl_response); //send me what var_dump return so I can hellp you accessing the array! echo var_dump($decodedresponse); Just this ? – btc4cash Aug 19 '17 at 11:14
  • Yes, that is all I get back. My php now looks like this: `$curl_response = curl_exec($curl); curl_close($curl); // echo ($curl_response); $decodedresponse = json_decode($curl_response); echo var_dump($decodedresponse);` And the entire response I get back in my JS when making this call is: `config:{method: "GET", transformRequest: Array(1), transformResponse: Array(1), jsonpCallbackParam: "callback", paramSerializer: ƒ, …} data:"NULL↵" headers:ƒ (d) status:200 statusText:"OK" __proto__:Object` – Alexandra Aug 23 '17 at 09:57
  • hum I'm not into JS so can't help for this. – btc4cash Aug 24 '17 at 05:01
-1

try again

CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • **[You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software)**. It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman May 16 '20 at 21:30