file_get_contents via HTTP - permission denied
Moderator: General Moderators
- 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
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
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().
try using ini_set() to set the user agent before calling file_get_contents().
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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 stumpedBurrito 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().
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
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.Burrito wrote:what is the url you're trying to open?
I'll play with it from here.
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).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.
Thanks for the support gents
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;
}
?>- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
Forgot about this thread..
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
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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
viewtopic.php?t=29312 may be of interest then. 