代理阻止file_get_contents

问题描述 投票:1回答:3

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

$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
  );
}

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

php proxy file-get-contents
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

0
投票

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

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

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


0
投票

当然,您还可以提及“上下文”以使file_get_contents识别代理。请查看我自己的问题以及我对here这个问题的回答

© www.soinside.com 2019 - 2024. All rights reserved.