在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);