简体   繁体   中英

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.

var_dump(http_response_code());

您可以使用此检查响应,然后根据发送的响应执行您需要的操作

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM