使用网址中的file_get_contents获取内容/文件时出现问题或反向地理编码时出现问题 [英] Problem in getting contents/ files using file_get_contents from url or Problem in reverse geo coding

查看:138
本文介绍了使用网址中的file_get_contents获取内容/文件时出现问题或反向地理编码时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在php脚本(使用xmlrpc)中使用Google api进行反向地址解析.我在本地系统中编写了以下代码,其工作正常,但是当我在网络服务器中尝试失败时.

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 ...
    }
}

我的操作失败了

我的本​​地php:php5.3.6和webser是5.2.9. 由于它一直失败,因此我将网址更改为 http://www.google.com 进行测试(不使用urlencode ),然后在网络服务器中也失败了.如果有人想纠正我的错误,请帮忙.谢谢

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

推荐答案

URL看起来已经正确地进行了URL编码,因此不要再次对其进行URL编码,这将导致URL在您的情况下不起作用.这就是为什么您得到FALSE返回值的原因.有关返回值的含义,请参见 file_get_contents Docs .

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 see file_get_contentsDocs.

取而代之的是获取URL:

Instead just get the URL:

$data = file_get_contents($url);

如果这行不通,则仅对查询参数使用urlencode:

If that does not work, only urlencode the query arguments:

$url = sprintf("http://maps.google.com/maps/geo?json&ll=%s,%s", 
                urlencode($lat), urlencode($long));

也请参见 url_encode Docs 统一资源标识符(RFC 2616第3.2节)以及统一资源标识符(URI):通用语法(RFC 2396).

如果您想进一步了解请求失败的原因,可以通过允许file_get_contents继续处理HTTP失败来获取更多信息(请

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.

此外,您还可以通过获取检查响应HTTP状态代码URL的标题或使用 $http_response_header 文档

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_headerDocs

最后是为什么file_get_contents不起作用? Wiki回答几乎涵盖了所有可能出错的地方以及如何处理.

And 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.

这篇关于使用网址中的file_get_contents获取内容/文件时出现问题或反向地理编码时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆