我不明白当我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