file_get_contents via HTTP - permission denied

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

file_get_contents via HTTP - permission denied

Post 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
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Use fsockopen instead.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Snoopy may work, as could cURL.

http://snoopy.sf.net
http://php.net/ref.curl
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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().
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

what is the url you're trying to open?

I'll play with it from here.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you haven't looked already, Snoopy uses the fsockopen() method, but in a nice class to interact with. :)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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;
}

?>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=29312 may be of interest then. ;)
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Great script! Thanks, feyd :D
Post Reply