使用 file_get_contents 从 url 获取内容/文件时出现问题或反向地理编码出现问题
我正在尝试在 php 脚本中使用 google api 进行反向地理编码(使用 xmlrpc)。我在本地系统中编写了以下代码,它工作正常,但是当我在我们的网络服务器中尝试时,它失败了。
function reverseGeoCode()
{
$url = "http://maps.google.com/maps/geo?json&ll=".$lat.",".$long;
$data = file_get_contents(urlencode($url));
if ($data == FALSE)
{
echo "failed";
}
else
{
echo "success";
// parsing the json/xml ...
}
}
o/p“失败”
我得到了本地 php 的 :php5.3.6,而 webser 是 5.2.9。 由于它不断失败,我将网址更改为 http://www.google.com 进行测试(不使用 urlencode ),然后它在网络服务器中也失败了。如果有人有想法纠正我的错误,请帮忙。提前致谢
I am trying reverse geocode using google api in php script(using xmlrpc).I wrote the following code in my local system its works fine,but when I try in our webserver it fails.
function reverseGeoCode()
{
$url = "http://maps.google.com/maps/geo?json&ll=".$lat.",".$long;
$data = file_get_contents(urlencode($url));
if ($data == FALSE)
{
echo "failed";
}
else
{
echo "success";
// parsing the json/xml ...
}
}
I got the o/p "failed"
my local php: php5.3.6 and webser is 5.2.9.
Since it continously failed i change the url to http://www.google.com for testing(with out using urlencode),then also it failed in webserver.If any one have idea to retify my error Please help.Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该 URL 看起来已经正确进行了 URL 编码,因此不要再次对其进行 URL 编码,这会导致 URL 不适用于您的情况。这就是为什么您会得到
FALSE
返回值。返回值的含义请参见file_get_contents
Docs 。相反,只需获取 URL:
如果这不起作用,则仅对查询参数进行 urlencode:
另请参阅
url_encode
文档 和 统一资源标识符(RFC 2616 第 3.2 节) 以及 统一资源标识符 (URI):通用语法 (RFC 2396)。如果您想了解请求失败的更多原因,可以通过允许
file_get_contents
在 HTTP 失败时继续处理来获取更多信息(请查看此答案如何在 4xx 上返回文档响应),如果您使用的 URL 已经正确并且您想了解有关服务器告诉您的更多信息,这会很有用。接下来,您还可以通过获取以下标头来检查响应 HTTP 状态代码: URL 或使用
$http_response_header
文档最后是 为什么不
file_get_contents
有效吗?维基百科的答案涵盖了几乎所有可能出错的地方以及如何处理它。The URL looks already properly URL-encoded, so don't URL-encode it again which results in an URL that does not work in your case. That's why you get the
FALSE
return value. For the meaning of the return values please seefile_get_contents
Docs.Instead just get the URL:
If that does not work, only urlencode the query arguments:
See as well
url_encode
Docs and Uniform Resource Identifiers (RFC 2616 Section 3.2) as well as Uniform Resource Identifiers (URI): Generic Syntax (RFC 2396).If you would like to find out more why the request failed, you can gain more information by allowing
file_get_contents
to proceed on HTTP failures (Please see this answer how to return the document on 4xx responses), that can be useful if the URL you're using is already correct and you would like to know more about what the server is telling you.Next to that you can check the response HTTP status code as well by getting the headers of the URL or by using
$http_response_header
DocsAnd finally there is the Why doesn't
file_get_contents
work? wiki answer covering nearly everything that can go wrong and how to deal with it.确保您已将 allow_url_fopen 设置为在你的 php.ini 中
Make sure you have allow_url_fopen set to on in your php.ini
您应该通过查看 PHP ini 设置
allow_url_fopen
的值来检查服务器上是否启用了“URL 包装器”编辑:您可以使用这段代码检查它是否启用
You should check that "URL wrappers" are enabled on the server by looking at the value of the PHP ini setting
allow_url_fopen
Edit: you can check whether it's enabled or not with this piece of code
如果你一切都做对了但仍然没有成功!并且您使用的是 Redhat 或 CentOs.. 然后尝试此命令
setsebool -P httpd_can_network_connect on
某些 RedHat 和 CentOs 安装会阻止 httpd 向外部服务器发送请求。
If you did everything right and still did not succeed! And you are using Redhat or CentOs.. Then try this command
setsebool -P httpd_can_network_connect on
Some RedHat and CentOs installations block httpd from sending requests to external servers.