使用cURL获取不带正文的http-statuscode? [英] Get http-statuscode without body using cURL?

查看:183
本文介绍了使用cURL获取不带正文的http-statuscode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析很多URL,以便仅获取其状态代码.

I want to parse a lot of URLs to only get their status codes.

所以我所做的是:

$handle = curl_init($url -> loc);
             curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
             curl_setopt($handle, CURLOPT_HEADER  , true);  // we want headers
             curl_setopt($handle, CURLOPT_NOBODY  , true);
             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
             $response = curl_exec($handle);
             $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
             curl_close($handle);

但是,只要将"nobody"选项设置为true,返回的状态代码就不正确(google.com返回302,其他网站返回303).

But as soon as the "nobody"-option is set to true, the returned status codes are incorrect (google.com returns 302, other sites return 303).

由于性能下降,无法将此选项设置为false.

Setting this option to false is not possible because of the performance loss.

有什么想法吗?

推荐答案

curl的默认HTTP请求方法是GET.如果只需要响应头,则可以使用HTTP方法HEAD.

The default HTTP request method for curl is GET. If you want only the response headers, you can use the HTTP method HEAD.

curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'HEAD');


根据@Dai的回答,NOBODY已经在使用HEAD方法.因此上述方法无效.


According to @Dai's answer, the NOBODY is already using the HEAD method. So the above method will not work.

另一个选择是使用fsockopen打开连接,使用fwrite编写标题.使用fgets读取响应,直到第一次出现\r\n\r\n为止,以获取完整的标头.由于只需要状态码,因此只需要读取前13个字符即可.

Another option would be to use fsockopen to open a connection, write the headers using fwrite. Read the response using fgets until the first occurrence of \r\n\r\n to get the complete header. Since you need only the status code, you just need to read the first 13 characters.

<?php
$fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
if ($fp) {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.google.com\r\n";
    $out .= "Accept-Encoding: gzip, deflate, sdch\r\n";
    $out .= "Accept-Language: en-GB,en-US;q=0.8,en;q=0.6\r\n";
    $out .= "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36\r\n";
    $out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    $tmp = explode(' ', fgets($fp, 13));
    echo $tmp[1];
    fclose($fp);
}

这篇关于使用cURL获取不带正文的http-statuscode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆