2017-05-04 29 views
1

我使用的API有時會返回HTTP/1.1 403 Forbidden消息。我應該如何處理代碼?如何處理HTTP/1.1 403禁止從PHP代碼

我想如果API返回403禁止跳過下面的代碼,如果它返回成功繼續下面的代碼。

任何人都可以告訴我如何在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> 

無法檢查狀態代碼。

+1

通常有來自您可以獲取的API的響應代碼。檢查它的200(一切正常)或403(禁止)。它是哪個API? – Qirel

+0

其[geoplugin](http://www.geoplugin.com/)API – Narayan

+0

在403返回像' 403禁止 <體BGCOLOR = 「白色」>

403的碼禁止


nginx的
' 應該怎麼檢查在這個網站碼狀態 – Narayan

回答

0

的幫助下PHP的「get_headers」功能,我很容易地獲得狀態代碼,並解決我的問題,

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

如果它將返回200,那麼我將繼續執行腳本,否則停止在那裏。

當我打印get_header的響應,將下面喜歡,

成功(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 
) 
1
var_dump(http_response_code()); 

您可以檢查與此響應,然後做你需要根據響應發送