PHP中使用CURL请求页面,使用fiddler进行抓包

在PHP中使用CURL访问页面:

<?php

$ch = curl_init('http://www.baidu.com'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
// 3. 执行并获取HTML文档内容
$output = curl_exec($ch);
// 4. 释放curl句柄
curl_close($ch);
echo $output;
?>

 可以显示页面,并且可以使用curl_getinfo函数获取请求头。

但不能直观的查看,而且无法使用Fiddler进行抓包。

经过搜索后终于找到解决办法:

Fiddler的抓包方式:打开Fiddler后,自动设置浏览器代理到127.0.0.1:8888。

所有浏览的网页均通过该端口进行转发,固可以抓包。

修改PHP代码:

<?php

$ch = curl_init('http://www.baidu.com'); 
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);
echo $output;
?>

 发现返回:

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.1
Host: www.baidu.com
Accept: */*
Connection: Keep-Alive

下行

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.                                                                                                                                                                                                                                                                                                                                                                                                                 

经过分析后发现,请求域名,必须以“/”结尾。

更改后代码

<?php

$ch = curl_init('http://www.baidu.com/'); 
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);
echo $output;
?>

效果:

Fiddler:

上行:

GET http://www.baidu.com/ HTTP/1.1
Host: www.baidu.com
Accept: */*
Connection: Keep-Alive

 

下行:

HTTP/1.1 200 OK
Date: Tue, 08 Oct 2013 15:51:19 GMT
Server: BWS/1.0
Content-Length: 11002
Content-Type: text/html;charset=utf-8
Cache-Control: private
BDPAGETYPE: 1
BDUSERID: 0
BDQID: 0x84be7be11e5c433f
Set-Cookie: BDSVRTM=1; path=/
Set-Cookie: H_PS_PSSID=3409_3381_2777_1426_2975_2980_3501; path=/; domain=.baidu.com
Set-Cookie: BAIDUID=68722482960D705B97CAF69731DF6C19:FG=1; expires=Tue, 08-Oct-43 15:51:19 GMT; path=/; domain=.baidu.com
Expires: Tue, 08 Oct 2013 15:51:19 GMT
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Connection: Keep-Alive

<!DOCTYPE html><!--STATUS OK--><html><head><meta ...略...

 

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

posted on 2013-10-08 23:53  CosmoKey  阅读(28068)  评论(0编辑  收藏  举报

导航