[英]How to handle HTTP/1.1 403 Forbidden from PHP code
我使用的 API 有时会返回HTTP/1.1 403 Forbidden消息。 我应该如何在代码中处理这个问题?
我想如果 API 返回403 Forbidden跳过下面的代码,如果它返回成功继续下面的代码。
谁能告诉我如何处理php代码中的HTTP/1.1 403 Forbidden ?
返回如下代码,
<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.
如果它将返回 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 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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.