I am using php file_get_contents to get a webpage.
When i am doing it from my localhost everything works good.
But as soon as i do it from my shared hosting i get this error message:
ERROR
Access Denied
Access Denied by security policy
The security policy for your network prevents your request from being allowed at this time. Please contact your administrator if you feel this is incorrect.
this is the header i get back
array(4) { [0]=> string(22) "HTTP/1.0 403 Forbidden" [1]=> string(23)
"Content-Type: text/html" [2]=> string(17) "Connection: close" [3]=> string(19)
"Content-Length: 353" }
the code is straightforward
$page = file_get_contents('http://www.somedomain.com/');
if i try for current page it works
$page = file_get_contents('http://stackoverflow.com/questions/10565054/error-access-denied-when-using-php-file-get-content');
any ideas ?
The remote webserver is rejecting your request. There's no telling speficically why it is denying you. It could the server is configured to deny an IP range that includes wherever your shared hosting is. It could be that the server requires certain header information (like a host entry, or a certain user_agent, or something).Try setting some HTTP context stuff to appease the server you're trying to contact. Here's an example from the file_get_contents page.
function file_get_contents_utf8($fn) {
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"Content-Type: text/html; charset=utf-8"
)
);
$context = stream_context_create($opts);
$result = @file_get_contents($fn,false,$context);
return $result;
}
If you can access the target from your browser, try duplicating allt he headers your browser is sending. Most browsers have some sort of plugin to show you exactly what headers are going out on the request.