请求如下:
echo get('http://www.baidu.com');exit;
function get($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。
$header = ['User-Agent: php test']; //设置一个你的浏览器agent的header
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 1); //返回response头部信息
curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HEADER_OUT); //官方文档描述是“发送请求的字符串”,其实就是请求的header。这个就是直接查看请求header,因为上面允许查看
curl_close($ch);
return $result;
}
结果如下
GET / HTTP/1.1
Host: www.baidu.com
Accept: */*
User-Agent: php test
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Tue, 04 Jul 2017 01:25:19 GMT
Content-Type: text/html
Content-Length: 2381
Last-Modified: Mon, 23 Jan 2017 13:27:32 GMT
Connection: Keep-Alive
ETag: "588604c4-94d"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
Accept-Ranges: bytes
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8>
......
后面很多,就是百度首页的所有HTML