Narayan
Narayan

Reputation: 1668

How to handle HTTP/1.1 403 Forbidden from PHP code

I am using the API sometimes it will return the HTTP/1.1 403 Forbidden message. How should I handle this in code?

I want if API return 403 Forbidden skip the below code and if its return the success continue with the below code.

Anyone can tell me how to handle the HTTP/1.1 403 Forbidden in php code?

Return a code as below,

<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

unable to check status code.

Upvotes: 1

Views: 2221

Answers (2)

Narayan
Narayan

Reputation: 1668

With the help of PHP's "get_headers" function I have get the status code easily and solve my problem,

get_headers($URL, 1); // will return all the headers sent by the server in response.

If it will return 200 then I will continue execute the script otherwise stop there.

When I print the response of get_header it will like below,

For Success(200) response as below,

Array
(
    [0] => HTTP/1.1 200 OK
    [Date] => Thu, 04 May 2017 12:38:41 GMT
    [Content-Type] => text/plain; charset=utf-8
    [Content-Length] => 725
    [Connection] => close
    [Server] => Apache
    [Cache-Control] => max-age=0
    [Expires] => Thu, 04 May 2017 12:38:41 GMT
)

And For 403 Forbidden

Array
(
    [0] => HTTP/1.1 403 Forbidden
    [Server] => geoPlugin
    [Date] => Thu, 04 May 2017 12:41:30 GMT
    [Content-Type] => text/html
    [Connection] => close
)

Upvotes: 0

Exprator
Exprator

Reputation: 27513

var_dump(http_response_code());

you can check the response with this and then do what you need according to the response sent

Upvotes: 1

Related Questions