Page 1 of 1

file_get_contents via HTTP - permission denied

Posted: Mon Jan 08, 2007 4:06 am
by aaronhall
Hey guys -- I'm about to pull my hair out. I'm getting a "Permission denied" error when using file_get_contents to open a public google maps geocoding url via http. phpinfo() reports that allow_url_fopen is enabled. Even though it won't let me open a Google url, It will let me open a file from my own site over HTTP. Any ideas? Thanks for any help

Posted: Mon Jan 08, 2007 5:15 am
by bokehman
Use fsockopen instead.

Posted: Mon Jan 08, 2007 9:10 am
by feyd
Snoopy may work, as could cURL.

http://snoopy.sf.net
http://php.net/ref.curl

Posted: Mon Jan 08, 2007 3:21 pm
by aaronhall
Thanks both of you for the reply. I'll probably have to the the socket route because I'm not sure that they have cURL enabled; I'll have to check. Thanks again for your time guys.

Posted: Mon Jan 08, 2007 3:27 pm
by Burrito
I'm guessing it's looking for a specific user agent (or a pool of agents).

try using ini_set() to set the user agent before calling file_get_contents().

Posted: Mon Jan 08, 2007 3:55 pm
by aaronhall
Burrito wrote:I'm guessing it's looking for a specific user agent (or a pool of agents).

try using ini_set() to set the user agent before calling file_get_contents().
The error I'm getting is actually being thrown by PHP, and not by Google. While allow_url_fopen is set to true, the host somehow disabled HTTP requests with file_get_contents outside of the current domain. I'm stumped

Posted: Mon Jan 08, 2007 4:04 pm
by Burrito
what is the url you're trying to open?

I'll play with it from here.

Posted: Mon Jan 08, 2007 10:37 pm
by AKA Panama Jack
If they have CURL installed you can use it to virtually duplicate that function. Some hosting companies have disabled all getting of data through external URLs using PHP functions. They usually have CURL installed and want you to use that instead.

Posted: Tue Jan 09, 2007 12:46 am
by aaronhall
Burrito wrote:what is the url you're trying to open?

I'll play with it from here.
It's the google maps geocoding URL (http://www.google.com/apis/maps/documen ... TP_Request), but I don't think google is even receiving the request -- the file_get_contents operation fails with a "Permission denied" error. I'm not getting this error on my local setup, so I'm almost certain it's a limitation set by the host.
AKA Panama Jack wrote:If they have CURL installed you can use it to virtually duplicate that function. Some hosting companies have disabled all getting of data through external URLs using PHP functions. They usually have CURL installed and want you to use that instead.
I haven't gotten around to checking yet, but if they don't, I guess I'll resort to using sockets (if that's not also restricted).

Thanks for the support gents :D

Posted: Tue Jan 09, 2007 12:53 am
by feyd
If you haven't looked already, Snoopy uses the fsockopen() method, but in a nice class to interact with. :)

Posted: Tue Jan 09, 2007 5:31 am
by bokehman

Code: Select all

<?php

echo GET('http://www.google.com/apis/maps/documentation/#Geocoding_HTTP_Request');

function GET($url)
{
    extract(parse_url($url));
    @$scheme or extract(parse_url('http://'.$url));
    $out = "GET ".(@$path?$path:'/').(@$query?'?'.$query:'')." HTTP/1.0\r\n";
    $out .= "Host: $host\r\n";
    $out .= "Connection: Close\r\n\r\n";
    $fp = @fsockopen($host, (@$port?$port:80), $es, $en, 5) or die('I could not connect');
    fwrite($fp, $out);
    $return = false;
    while (!feof($fp))
    {
        $s = fgets($fp, 128);
        if($return === false && $s === "\r\n")
        {
            $return = '';
        }
        elseif($return === false && preg_match('/^Location:(.+)/i', $s, $m) === 1)
        {
            fclose($fp);
            return GET(trim($m[1]));
        }
        elseif($return !== false)
        {
             $return .= $s;
        }
    }
    fclose($fp);
    return $return;
}

?>

Posted: Wed Jan 10, 2007 2:24 am
by aaronhall
Forgot about this thread.. :oops: Many thanks to both of you for the reply.

bokehman: Thanks for script -- it works great, except that it looks like fsockopen() is restricted on this host as well -- similar "permission denied" error when trying to connect to google. This is the same server that evilwalrus is on, and it will connect to and open up http://www.evilwalrus.org. Additionally, it will connect to google from my local server. So I'm guessing Snoopy isn't going to work either, feyd.

It turns out after all that the production server will allow file_get_contents on remote URLs, so it won't be a problem in the scope of the project I'm working on, but I still want to try to get cURL to work in case this happens again in the future.

Thanks again guys -- you are the best

Posted: Wed Jan 10, 2007 10:11 am
by feyd
viewtopic.php?t=29312 may be of interest then. ;)

Posted: Wed Jan 10, 2007 7:47 pm
by aaronhall
Great script! Thanks, feyd :D