$ch = curl_init();// set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/"); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); // grab URL and pass it to the browser curl_exec($ch);// close cURL resource, and free up system resources curl_close($ch);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回$header=curl_exec($ch); //执行正则匹配$header // close cURL resource, and free up system resources curl_close($ch);
如果说要更智能的话 就再通过header头中显示的body大小来判断是否要获取body
当然如果只字需要header那很简单 http协议中有header的方法 直接发送一个header请求就是,服务器不会返回body
curl_setopt($ch, CURLOPT_URL, "http://www.baidu.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
// grab URL and pass it to the browser
curl_exec($ch);// close cURL resource, and free up system resources
curl_close($ch);
//执行正则匹配$header
// close cURL resource, and free up system resources
curl_close($ch);