0

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

hakre
  • 193,403
  • 52
  • 435
  • 836
Jisson
  • 3,566
  • 8
  • 38
  • 71
  • What is the error? The PHP error? What is the output of file_get_contents – andho Aug 05 '11 at 09:33
  • Oh sorry i forget to specify am new in php.There is no error the problem is my script didnot fetch the contents of url,it returns FALSE – Jisson Aug 05 '11 at 09:35
  • possible duplicate of [Why doesn't file_get_contents work?](http://stackoverflow.com/questions/6724467/why-doesnt-file-get-contents-work) – hakre Aug 05 '11 at 10:13

4 Answers4

2

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.

Instead just get the URL:

$data = file_get_contents($url);

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

See as well url_encodeDocs 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_headerDocs

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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Still the same problem.Even with url http://www.google.com I have the same problem – Jisson Aug 05 '11 at 09:50
  • @Jisson: I added that aspect to the answer incl. the most general answer at the end which covers file_get_contents trouble shooting in detail. And regarding the URL: You need to know which one is the right one first. I can not tell you if using `google.com` is the solution or just an *additional* problem you're introducing. You first need to find out why `file_get_contents` returns FALSE, e.g. understanding that return value. Then you can test for headers and/or check if PHP allows you to even open remote URLs with `file_get_contents`. – hakre Aug 05 '11 at 10:08
  • Thanks for u r quick response – Jisson Aug 05 '11 at 10:18
  • the problem is with server configuration – Jisson Aug 05 '11 at 10:19
  • Due to security issues our webserver partner didn't ready to change the server configuraion,Is there any alternate way to acces the url contents of google map with out change server congfiguration – Jisson Aug 06 '11 at 05:27
  • @Jisson: *Which* server configuration is preventing you doing so? Is that a specific setting or is remote access blocked by firewall? – hakre Aug 06 '11 at 08:29
1

Make sure you have allow_url_fopen set to on in your php.ini

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • Due to security issues our webserver partner didn't ready to change the server configuraion,Is there any alternate way to acces the url contents of google map with out change server congfigurations. – Jisson Aug 06 '11 at 05:15
  • No not really. Perhaps through curl, if that is enabled. – TJHeuvel Aug 06 '11 at 08:31
1

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

echo php_ini_get('allow_url_fopen') ? 'Enabled' : 'Disabled';
James C
  • 14,047
  • 1
  • 34
  • 43
  • The function php_ini_get leads error ,I tried ini_get('allow_url_fopen').Thank u for u r response,it help me detect the problem – Jisson Aug 05 '11 at 10:16
0

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.

Yacoub Oweis
  • 352
  • 4
  • 11