我有我的虚拟托管网络服务器。我使用 VirtualBox 安装它,它使用 Ubuntu Server 系统。最近,我需要从 Google Maps Geocode 服务中获取数据。首先,我尝试使用下一个代码:
file_get_contents(URL);
收到超时错误后,我也尝试使用 cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/api/geocode/json?address=" . $gm_address . "&sensor=false");
$result = curl_exec($ch);
curl_close($ch);
再一次,我得到了一个超时错误。
我怀疑 Ubuntu 不允许 PHP 调用其他网站。我不是 Linux 或 Ubuntu 专家,所以我不知道如何处理防火墙设置或允许 PHP 进行这些调用的设置。
简而言之,如何更改允许 PHP 从其他网站获取数据的设置?
最佳答案
试试这个 cURL 代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/api/geocode/json?address=" . $gm_address . "&sensor=false");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
文本现在是否出现在
$result
中? ?
关于PHP5 无法接收来自其他网站的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8824589/