[英]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.