我是卷曲的新人,我无法找到答案。 我想获取页面的http状态,所以我使用
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
但如果有重定向(例如301重定向到另一个页面,它会给我200个答案),这不起作用,因为curlinfo_http_code给出了最后收到的HTTP代码
知道如何获得第一个收到的http代码吗? 感谢
答案 0 :(得分:1)
您需要使用CURLOPT_FOLLOWLOCATION = 0配置curl
答案 1 :(得分:0)
我实现这一目标的方法是让PHP查询带有不同返回码的INI文件,并用可读的东西替换它们
<?php
$ch = curl_init(); // create cURL handle (ch)
if (!$ch) {
die("Couldn't initialize a cURL handle");
}
// set some cURL options
$ret = curl_setopt($ch, CURLOPT_URL, "http://mail.yahoo.com");
$ret = curl_setopt($ch, CURLOPT_HEADER, 1);
$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// execute
$ret = curl_exec($ch);
if (empty($ret)) {
// some kind of an error happened
die(curl_error($ch));
curl_close($ch); // close cURL handler
} else {
$info = curl_getinfo($ch);
curl_close($ch); // close cURL handler
if (empty($info['http_code'])) {
die("No HTTP code was returned");
} else {
// load the HTTP codes
$http_codes = parse_ini_file("HTMLCodes.ini");
// echo results
echo "The server responded: <br />";
echo $http_codes[$info['http_code']];
}
}
?>
example of the ini file with return codes
[Informational 1xx]
100="Continue"
101="Switching Protocols"
[Successful 2xx]
200="OK"
201="Created"
202="Accepted"
203="Non-Authoritative Information"
204="No Content"
205="Reset Content"
206="Partial Content"
[Redirection 3xx]
300="Multiple Choices"
301="Moved Permanently"
302="Found"
303="See Other"
304="Not Modified"
305="Use Proxy"
306="(Unused)"
307="Temporary Redirect"