问题描述
我面临一个不寻常的卷曲行为。对于给定的页面,我有时获得HTTP响应代码为200,有时我获得0作为HTTP响应代码。我无法理解此网页是否有效。如果你尝试给定的代码,请尝试至少5-10次,以便你可以看到差异。
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 ?
推荐答案
CURL请求没有完成,因此没有响应代码。
此原因可能是无效的主机名(无法解析),格式不正确的网址,超时等。
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.
您应该能够CURL错误代码,如CodeCaster的注释和curl_error / curl_errno文档。
You should be able to get the CURL error code as in CodeCaster's comment and curl_error / curl_errno docs.
一旦CURL请求正确完成,响应代码(从服务器)应该可用和有意义。
Once the CURL request completed properly, then a response code (from the server) should be available and meaningful.
这篇关于CURL在不同时间为同一页返回响应代码200和0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!