Php,谷歌距离矩阵总是返回false

时间:2015-11-04 11:23:24

标签: php json google-maps

这是我尝试的链接:

https://maps.googleapis.com/maps/api/distancematrix/json?origins=47.02842388681676|18.51792812347412&destinations=47.4735911253571|20.596618652343803&key=*******&language=en&units=metric

这适用于浏览器。但是从PHP中给出了FALSE:

  $ch = curl_init();
  $url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins='.urlencode($lat1).'|'.urlencode($long1).'&destinations='.urlencode($lat2).'|'.urlencode($long2).'&key='.urlencode('**********').'&language=en&units=metric';
  echo $url;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $c = curl_exec($ch);
  curl_close($ch);
  //$c = json_decode($c);
  echo '<pre>'; var_dump ($c);die;

1 个答案:

答案 0 :(得分:1)

您可以尝试使用file_get_contents:

$content = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=47.02842388681676|18.51792812347412&destinations=47.4735911253571|20.596618652343803&key=*******&language=en&units=metric');
$content = json_decode($content);

print_r($content);