new_one
new_one

Reputation: 140

How to handle 403 error in file_get_contents()?

I'm getting 403 errors when using file_get_contents(),

I want to handle this error like this,

if(required_function(file_get_contents($url))){  //detects there is a 403
    error
// do some special set of tasks
}else{
    //run normally
}

I tried to read the error since the url shows the error when I pasted in the browser , but is not getting in to file_get_contents() so I failed. I don't think changing user agent will work because systems may still be able to detect this is a script ,so I realized if I could detect the 403 error, the script will not crash.

any ideas ?

please help , I'm new to programming. thanks a lot.

Upvotes: 2

Views: 5089

Answers (2)

Michael Chen
Michael Chen

Reputation: 5668

I just encountered similar problem and resolved it. My solution covers much more cases:

Q: How to do POST in PHP, not using cURL?
A: Use file_get_contents().

Q: How to make file_get_contents() not to complain with error HTTP status?
A: Set ingore_errors=>TRUE in options passed to file_get_contents().

Q: How to retrieve http status in response?
A: After file_get_contents() call, evaluate $http_response_header

Q: How to retrieve response body even for an error http response code?
A: Set ignore_errors=>TRUE and file_get_contents() will return body.

Here is the code example:

$reqBody = '{"description":"test"}';
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => "Content-Type: application/json\r\n",
        'ignore_errors' => TRUE,
        'content' => $reqBody
    )
);

$context = stream_context_create($opts);
$url = 'http://mysite';
// with ignore_errors=true, file_get_contents() won't complain
$respBody = file_get_contents($url, false, $context);

// evaluate the response header, the way you want.
// In particular it contains http status code for response
var_dump($http_response_header);

// with ignore_errors=true, you get respBody even for bad http response code
echo $respBody;

Upvotes: 3

Tuğca Eker
Tuğca Eker

Reputation: 1493

Personally I'm suggesting you to use cURL instead of file_get_contents. file_get_contents is great for basic content oriented GET requests. But the header, HTTP request method, timeout, redirects, and other important things do not matter for it.

Nevertheless, to detect status code (403, 200, 500 etc.) you can use get_headers() call or $http_response_header auto-assigned variable.

$http_response_header is a predefined variable and it is updated on each file_get_contents call.

Following code may give you status code (403, 200 etc) directly.

preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#", $http_response_header[0], $match);
$statusCode = intval($match[1]);

For more information and content of variable please check official documentation

$http_response_header — HTTP response headers

get_headers — Fetches all the headers sent by the server in response to a HTTP request

(Better Alternative) cURL

Warning about $http_response_header, (from php.net)

Note that the HTTP wrapper has a hard limit of 1024 characters for the header lines. Any HTTP header received that is longer than this will be ignored and won't appear in $http_response_header. The cURL extension doesn't have this limit.

Upvotes: 4

Related Questions