7

我正在使用谷歌地理编码服务。我有一个调用 Google Maps API 并接收 JSON 数据的 PHP 应用程序。调用谷歌地图主机的功能会挂起,直到超时,但只有当我推送到 Godaddy 虚拟专用服务器时。

我已经 ssh 进入服务器并编辑了 php.ini 我将“安全模式”更改为“关闭”

我收到此错误消息:

消息:file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=xYxY&sensor=false):无法打开流:连接超时

这在我的 WAMP 服务器中运行良好,但在实时服务器上失败。任何想法为什么?

4

4 回答 4

10

我找到了答案。一周过去了,现在?我希望其他人找到这个解决方案。GoDaddy 的虚拟专用服务器启用了 ipv6,但谷歌地图 API 却没有。所以告诉 Curl 强制执行 v4 请求,如下所示:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

功劳部分归功于我发现以下信息的博客:

http://www.businesscorner.co.uk/disable-ipv6-in-curl-and-php/

于 2012-06-29T04:41:23.247 回答
3

除了禁用 IPv6,您还可以尝试通过一个 IP 版本连接,然后在第一次尝试失败时切换到另一个版本。这使您的实现对您和远程端的临时路由问题更加健壮。

You can get this behavior in file_get_contents() by binding the connection to an interface with either inet6 or inet, and then try the other address family if the first attempt returns FAIL. I wrote up how to make file_get_contents() more routing-robust and dual-stack for anyone that is interested. I also shows you how to force connections to use IPv4 or IPv6 if you prefer to go down that route.

于 2016-12-08T14:25:28.923 回答
-1

使用 curl 获取外部数据。由于安全风险,许多共享服务器会阻止将 file_get_contents 用于外部数据 (http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen)。

网上有很多 curl 的例子。

于 2012-06-20T07:24:11.420 回答
-1

检查您的网络。Google 不会阻止此类请求。

在这里检查。

于 2012-06-20T07:26:25.460 回答