1

在我的业务中,我必须为我的应用程序使用谷歌地图(计算距离)我们目前使用代理配置脚本。在我的应用程序中,我使用该方法来查询file_get_contentsGoogle Map。

$url = 'http://maps.google.com/maps/api/directions/xml?language=fr&origin='.$adresse1.'&destination='.$adresse2.'&sensor=false';;

  $xml=file_get_contents($url);
  $root = simplexml_load_string($xml);
  $distance=$root->route->leg->distance->value;
  $duree=$root->route->leg->duration->value; 
  $etapes=$root->route->leg->step;
  return array(
     'distanceEnMetres'=>$distance,
     'dureeEnSecondes'=>$duree,
     'etapes'=>$etapes,
     'adresseDepart'=>$root->route->leg->start_address,
     'adresseArrivee'=>$root->route->leg->end_address
  );
}

但是使用代理我有一个未知的主机错误。(我测试了我的家,代码工作正常)。我想知道是否有办法考虑我在浏览网页时标识自己的代理?

4

3 回答 3

0

你可以用cURL做到这一点。它比简单的调用更冗长file_get_contents(),但更可配置:

$url = 'http://maps.google.com/maps/api/directions/xml?language=fr&origin='.$adresse1.'&destination='.$adresse2.'&sensor=false';

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_PROXY, ''); // your proxy address (and optional :port)
curl_setopt($handle, CURLOPT_PROXYUSERPWD, ''); // credentials in username:password format (if required)
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($handle);
curl_close($handle);

//continue logic using $xml as before
于 2013-09-13T10:34:55.060 回答
0

谷歌对其 api 的每个时间段的查询数量有限制

首先,您必须在您身边缓存结果,并在查询之间添加暂停

https://developers.google.com/maps/documentation/business/articles/usage_limits

于 2013-09-13T10:33:35.833 回答
0

当然,您也可以提及“上下文”以使 file_get_contents 识别代理。请在此处查看我自己的问题和我自己对该问题的回答

于 2015-07-29T14:45:45.750 回答