Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-07-11 20:00:42
Original
4204 Leute haben es durchsucht

在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;
?>
Nach dem Login kopieren

 可以显示页面,并且可以使用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;
?>
Nach dem Login kopieren

 发现返回:

HTTP/1.1 504 Fiddler - Receive Failure <span style="color: #008080;">Date</span>: 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 <span style="color: #0000ff;">return</span> a response <span style="color: #0000ff;">for</span> this request.Server returned 0 bytes. 
Nach dem Login kopieren

抓包信息为:

上行

GET http:<span style="color: #008000;">//</span><span style="color: #008000;">www.baidu.com HTTP/1.1</span>
Host: www.baidu.<span style="color: #000000;">com
Accept</span>: *<span style="color: #008000;">/*</span><span style="color: #008000;">
Connection: Keep-Alive</span>
Nach dem Login kopieren

下行

HTTP/1.1 504 Fiddler -<span style="color: #000000;"> Receive Failure
</span><span style="color: #008080;">Date</span>: Tue, 08 Oct 2013 15:22:50<span style="color: #000000;"> GMT
Content</span>-Type: text/html; charset=UTF-8<span style="color: #000000;">
Connection</span>:<span style="color: #000000;"> close
Timestamp</span>: 23:22:50.647<span style="color: #000000;">

[Fiddler] ReadResponse() failed</span>: The server did not <span style="color: #0000ff;">return</span> a response <span style="color: #0000ff;">for</span> this request.Server returned 0 bytes.                                                                                                                                                                                                                                                                                                                                                                                                                 
Nach dem Login kopieren

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

更改后代码

<?<span style="color: #000000;">php

</span><span style="color: #800080;">$ch</span> = curl_init('<span style="color: #ff0000;">http://www.baidu.com/</span>'<span style="color: #000000;">); 
curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_RETURNTRANSFER, 1<span style="color: #000000;">);
curl_setopt(</span><span style="color: #800080;">$ch</span>, CURLOPT_HEADER, 1<span style="color: #000000;">);

curl_setopt(</span><span style="color: #800080;">$ch</span>,CURLOPT_PROXY,'127.0.0.1:8888');<span style="color: #008000;">//</span><span style="color: #008000;">设置代理服务器 </span>
curl_setopt(<span style="color: #800080;">$ch</span>,CURLOPT_SSL_VERIFYPEER,0);<span style="color: #008000;">//</span><span style="color: #008000;">若PHP编译时不带openssl则需要此行 
// 3. 执行并获取HTML文档内容</span>
<span style="color: #800080;">$output</span> = curl_exec(<span style="color: #800080;">$ch</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 4. 释放curl句柄</span>
curl_close(<span style="color: #800080;">$ch</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$output</span><span style="color: #000000;">;
</span>?>
Nach dem Login kopieren

效果:

Fiddler:

上行:

GET http:<span style="color: #008000;">//</span><span style="color: #008000;">www.baidu.com/ HTTP/1.1</span>
Host: www.baidu.<span style="color: #000000;">com
Accept</span>: *<span style="color: #008000;">/*</span><span style="color: #008000;">
Connection: Keep-Alive</span>
Nach dem Login kopieren

 

下行:

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

</span><!DOCTYPE html><!--STATUS OK--><html><head><meta ...略...
Nach dem Login kopieren

 

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

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage