function curl_get_file_contents($url) $user_agent = mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0; 4399box.560; .net4.0c; .net4.0e); $c = curl_init(); curl_setopt($c, curlopt_returntransfer, 1); curl_setopt($c, curlopt_hea
function curl_get_file_contents($url){
$user_agent = mozilla/4.0 (compatible; msie 8.0; windows nt 5.1; trident/4.0; 4399box.560; .net4.0c; .net4.0e);
$c = curl_init();
curl_setopt($c, curlopt_returntransfer, 1);
curl_setopt($c, curlopt_header, 1);
curl_setopt($c, curlopt_useragent, $user_agent);
curl_setopt($c, curlopt_url, $url);
$response = curl_exec($c);
$header_size = curl_getinfo($c, curlinfo_header_size);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($c);
if ($body) {
return array($header, $body);}
else {return false;}
}
附带昆爷建议直接得到header内容:
$c = curl_init();
curl_setopt($c, curlopt_returntransfer, 1);
curl_setopt($c, curlopt_useragent, $user_agent);
curl_setopt($c, curlopt_url, $url);
$response = curl_exec($c);
$header = curl_getinfo($c);
原文地址:curl下载文件得到header和body, 感谢原作者分享。