如何处理 HTTP/1.1 403 Forbidden from PHP 代码

How to handle HTTP/1.1 403 Forbidden from PHP code

我正在使用 API 有时它会 return HTTP/1.1 403 Forbidden 消息。我应该如何在代码中处理这个问题?

我想要 if API return 403 Forbidden 跳过下面的代码,如果 return 成功继续下面的代码。

谁能告诉我如何处理 HTTP/1.1 403 Forbidden in php 代码?

Return代码如下,

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

无法检查状态代码。

var_dump(http_response_code());

你可以用这个检查响应,然后根据发送的响应做你需要的

在PHP的“get_headers”功能的帮助下,我很容易获得状态代码并解决了我的问题,

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

如果它会 return 200 那么我将继续执行脚本,否则就停在那里。

当我打印 get_header 的响应时,它会像下面这样,

对于 Success(200) 响应如下,

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
)

并且对于 403 禁止

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