我不明白當我echo $ httpCode我總是得到0,我期待404當我改變$ html_brand成破碎的網址。有什麼我想念或不知道的?謝謝。PHP cURL HTTP代碼返回0
//check if url exist
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $html_brand);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 404) {
echo "The Web Page Cannot Be Found";
return;
}
curl_close($ch);
此外thx爲refencing followlocation,有時我得到301這是一個重定向問題。 – Ardeus 2012-04-20 07:16:21
即使$ response有html內容,它也很奇怪,但$ httpcode總是返回0,加上curl_error爲null。有可能出錯了。我只包含returntransfer,header(false),僅在設置中跟蹤位置。 – Ardeus 2012-05-02 12:08:07
非常有用!非常感謝你。 – 2017-08-04 11:53:29