簡體   English   中英

如何使用PHP curl_setopt檢查服務器錯誤類型?

[英]How to check server error type with PHP curl_setopt?

我正在嘗試通過使用PHP curl_setopt作為其他功能從另一個網站查詢數據,但是我不知道我的伴侶服務器何時出現任何錯誤(如下所述),是否有效。

    [Server Error 5xx]
    500="Internal Server Error"
    501="Not Implemented"
    502="Bad Gateway"
    503="Service Unavailable"
    504="Gateway Timeout"
    505="HTTP Version Not Supported"


   public function getNumber() {

        $url = "http://website/PalmHallServer_kl/!queryLuckNumberRecordByPeriods.action";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: Json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $contents = curl_exec($ch);
        $res = false;
        if ($contents) {
            $res = TRUE;
        } else {
            $res = FALSE;
        }
        echo json_encode(array("res" => $res, 'data' => $contents));
    }

首先告訴curl您想查看返回的標題

curl_setopt($ch, CURLOPT_HEADER, true); 

然后您可以使用獲得響應代碼

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

要查看響應標題中包含的所有信息,您可以執行

print_r(curl_getinfo($ch));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM