【问题标题】:How to handle HTTP/1.1 403 Forbidden from PHP code如何处理 PHP 代码中的 HTTP/1.1 403 Forbidden
【发布时间】:2017-10-02 12:58:14
【问题描述】:

我正在使用 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>

无法检查状态码。

【问题讨论】:

  • 通常您可以从 API 中获取响应代码。检查它是 200(一切正常)还是 403(禁止)。它是哪个 API?
  • 它的geoplugin API
  • 在 403 上返回类似 &lt;html&gt; &lt;head&gt;&lt;title&gt;403 Forbidden&lt;/title&gt;&lt;/head&gt; &lt;body bgcolor="white"&gt; &lt;center&gt;&lt;h1&gt;403 Forbidden&lt;/h1&gt;&lt;/center&gt; &lt;hr&gt;&lt;center&gt;nginx&lt;/center&gt; &lt;/body&gt; &lt;/html&gt; 的代码我应该如何检查这个 html 代码中的状态

标签: php api http-status-code-403


【解决方案1】:
var_dump(http_response_code());

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

【讨论】:

    【解决方案2】:

    借助 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 禁止

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2021-10-10
      • 1970-01-01
      相关资源
      最近更新 更多