PHP的curl获取header信息
<p>PHP的curl功能十分强大,简单点说,就是一个PHP实现浏览器的基础。</p><p>最常用的可能就是抓取远程数据或者向远程POST数据。但是在这个过程中,调试时,可能会有查看header的必要。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">echo</span> get('http://www.baidu.com');<span style="color: rgba(0, 0, 255, 1)">exit</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> get(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(128, 0, 128, 1)">$ch</span> =<span style="color: rgba(0, 0, 0, 1)"> curl_init();
curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_HTTPGET, <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">);
curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_RETURNTRANSFER, 1); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。</span>
<span style="color: rgba(128, 0, 128, 1)">$header</span> = ['User-Agent: php test']; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置一个你的浏览器agent的header</span>
curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_HTTPHEADER, <span style="color: rgba(128, 0, 128, 1)">$header</span><span style="color: rgba(0, 0, 0, 1)">);
curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_HEADER, 1); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回response头部信息</span>
curl_setopt(<span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLINFO_HEADER_OUT, <span style="color: rgba(0, 0, 255, 1)">true</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header</span>
<span style="color: rgba(0, 0, 0, 1)">
curl_setopt(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLOPT_URL, <span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$result</span> = curl_exec(<span style="color: rgba(128, 0, 128, 1)">$ch</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> curl_getinfo(<span style="color: rgba(128, 0, 128, 1)">$ch</span>, CURLINFO_HEADER_OUT); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">官方文档描述是“发送请求的字符串”,其实就是请求的header。这个就是直接查看请求header,因为上面允许查看</span>
<span style="color: rgba(0, 0, 0, 1)">
curl_close(</span><span style="color: rgba(128, 0, 128, 1)">$ch</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$result</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p>结果如下,很清楚的让你知道在请求URL的过程中,发送的header和返回的header信息:</p>
<div class="cnblogs_code">
<pre>GET / HTTP/<span style="color: rgba(128, 0, 128, 1)">1.1</span><span style="color: rgba(0, 0, 0, 1)">
Host: www.baidu.com
Accept: </span>*<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
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</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/niuben/p/13095395.html
頁:
[1]