Search code examples
phpcurlgoogle-apigeocode

getting no response from php google geocode rest api request


I'm getting no response and no errors from this php code. Does anyone know what I'm doing wrong, please? It seems straightforward:

php:

$details_url = "https://maps.googleapis.com/maps/api/geocode/json?address=436+Grant+Street+Pittsburgh&sensor=false&key=mykey";
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HEADER, 0);
   $response = curl_exec($ch);
   print_r($response);

Solution

  • You never bothered to tell curl about your url. You should have

    $ch = curl_init($details_url);
                    ^^^^^^^^^^^^
    

    or

    curl_setopt($ch, CURLOPT_URL, $details_url);
    

    And note that print_r is not a good debug tool. You probably got a boolean false from curl_exec, which print_r won't display at all:

    php > $x = false;
    php > print_r($x);
    php > var_dump($x);
    bool(false)
    

    A better option would be

    $response = curl_exec($ch);
    if ($response === false) {
        die(curl_error($ch));
    }