cURL with Google Maps Directions API returning null

0

Here is the code I'm using:

          $arquivo = "destino.json";
          $info = file_get_contents($arquivo);
          $lendo = json_decode($info);

          foreach($lendo->rotas as $campo){

            echo "<b>Origem:</b> ".$campo->origem;
            $origin = $campo->origem;
            echo "<br /><b>Destino:</b> ".$campo->destino;
            $destination = $campo->destino;

          }

          $key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
          //echo "<br><a href='".$url."' >Calcular Rota</a>";

          $url = "https://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&sensor=false&key=$key";

          echo "<br><a href='".$url."'>URL</a>";
          // create curl resource
          $ch = curl_init();
          // set url
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $output = curl_exec($ch);
          curl_close($ch);

          $arr = json_decode($output, TRUE);

          echo "<br><br><br>";
          echo "";
          print_r($arr);
    
asked by anonymous 19.12.2016 / 14:30

1 answer

0

Google Maps API uses HTTPS , then add in cURL as follows:

      curl_setopt($ch, CURLOPT_TIMEOUT, 5);
      curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);

In addition, the past address can not contain spaces, so:

  $origin = str_replace(" ", "", $campo->origem);
  $destination = str_replace(" ", "", $campo->destino
    
19.12.2016 / 14:30