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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM