如何处理HTTP/1.1 403禁止从PHP代码

问题描述:

我使用的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禁止

403的码禁止


nginx的

的帮助下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 
) 

var_dump(http_response_code()); 

您可以检查与此响应,然后做你需要根据响应发送