问题
I am facing one unusual behavior of curl. For a given page, I some times get HTTP response code as 200 and sometimes I get 0 as HTTP response code. I am not able to understand whether this page is valid or not. If you try the given code, please try it for at least 5-10 times so that you can see the difference.
function print_info()
{
$url = 'bart.no';
$arr = array(
'bart.no',
'bolandirekt.nu',
'ekompassen.com',
'ekompassen.nu',
);
foreach ($arr as $url)
{
echo "<br/>URL: " . $url;
$temp = str_replace(array("www.", "http://", "https://"), "", strtolower($url));
// From this array it will be decided which is to prepend
$pre_array = array("", "www.", "https://", "http://", "https://www.", "http://www.");
$status_code = array();
// For each Value Status will be stored
foreach ($pre_array as $pre)
{
$options = array(
CURLOPT_RETURNTRANSFER => TRUE, // return web page
CURLOPT_HEADER => TRUE, // don't return headers
CURLOPT_FOLLOWLOCATION => FALSE, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => FALSE, // set referer on redirect
CURLOPT_SSL_VERIFYHOST => FALSE, //ssl verify host
CURLOPT_SSL_VERIFYPEER => FALSE, //ssl verify peer
CURLOPT_NOBODY => FALSE,
CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect
CURLOPT_TIMEOUT => 20, // timeout on response
);
// Initializing Curl
$ch = curl_init($pre . $temp);
// Set Curl Options
curl_setopt_array($ch, $options);
// Execute Curl
$content = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "<pre/>";
if ($code == 200)
{
print_r(curl_getinfo($ch));
break;
}
curl_close($ch);
}
}
}
So my final doubt is : Why I am getting response code 200 for the pages which are not existing Or not opening in browser ? Also, why sometimes I get response code 0 and sometimes response code 200 for the same page even if I keep time interval between requests ?
回答1:
The CURL request did not complete, thus there's no response code. The reason for this may be an invalid host name (can't resolve), malformed URL, timeout, etc.
You should be able to get the CURL error code as in CodeCaster's comment and curl_error / curl_errno docs.
Once the CURL request completed properly, then a response code (from the server) should be available and meaningful.
来源:https://stackoverflow.com/questions/12781590/curl-returns-response-code-200-and-0-for-a-same-page-at-different-time