在PHP中使用CURL访问页面:
可以显示页面,并且可以使用curl_getinfo函数获取请求头。
但不能直观的查看,而且无法使用Fiddler进行抓包。
经过搜索后终于找到解决办法:
Fiddler的抓包方式:打开Fiddler后,自动设置浏览器代理到127.0.0.1:8888。
所有浏览的网页均通过该端口进行转发,固可以抓包。
修改PHP代码:
发现返回:
HTTP/1.1 504 Fiddler - Receive Failure Date: Tue, 08 Oct 2013 15:22:50 GMT Content-Type: text/html; charset=UTF-8 Connection: close Timestamp: 23:22:50.647 [Fiddler] ReadResponse() failed: The server did not return a response for this request.Server returned 0 bytes.
抓包信息为:
上行
GET http://www.baidu.com HTTP/1.1Host: www.baidu.comAccept: */*Connection: Keep-Alive
下行
HTTP/1.1 504 Fiddler - Receive FailureDate: Tue, 08 Oct 2013 15:22:50 GMTContent-Type: text/html; charset=UTF-8Connection: closeTimestamp: 23:22:50.647[Fiddler] ReadResponse() failed: The server did not return a response for this request.Server returned 0 bytes.
经过分析后发现,请求域名,必须以“/”结尾。
更改后代码
效果:
Fiddler:
上行:
GET http://www.baidu.com/ HTTP/1.1Host: www.baidu.comAccept: */*Connection: Keep-Alive
下行:
HTTP/1.1 200 OKDate: Tue, 08 Oct 2013 15:51:19 GMTServer: BWS/1.0Content-Length: 11002Content-Type: text/html;charset=utf-8Cache-Control: privateBDPAGETYPE: 1BDUSERID: 0BDQID: 0x84be7be11e5c433fSet-Cookie: BDSVRTM=1; path=/Set-Cookie: H_PS_PSSID=3409_3381_2777_1426_2975_2980_3501; path=/; domain=.baidu.comSet-Cookie: BAIDUID=68722482960D705B97CAF69731DF6C19:FG=1; expires=Tue, 08-Oct-43 15:51:19 GMT; path=/; domain=.baidu.comExpires: Tue, 08 Oct 2013 15:51:19 GMTP3P: CP=" OTI DSP COR IVA OUR IND COM "Connection: Keep-Alive
Web:
$ch = curl_init('https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.min.js');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 1);curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888');//设置代理服务器 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);//若PHP编译时不带openssl则需要此行 // 3. 执行并获取HTML文档内容$output = curl_exec($ch);// 4. 释放curl句柄curl_close($ch);