2011-04-13 408 views
0

我試圖重現使用PHP從WireShark捕獲的POST請求。這個POST請求是由Flash(.swf)對象發送的,所以在配置標題時有點複雜。使用x-amf(Flash)請求標頭髮送POST請求

它最終沒有打印出任何東西,所以一定會出現我看不到的PHP代碼錯誤。

這裏是Wireshark的捕獲:

POST /engine/ HTTP/1.1\r\n 
Host: abcdef.com\r\n 
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0\r\n 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n 
Accept-Language: en-us,en;q=0.5\r\n 
Accept-Encoding: gzip, deflate\r\n 
Accept-Charset: UTF-8,*\r\n 
Keep-Alive: 115\r\n 
Connection: keep-alive\r\n 
Cookie: __utma=77520967.190998754.1302600802.1302605710.1302693085.3; __utmz=77520967.1302600802.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); PHPSESSID=vqtt7v2l5h10nd06fdsuii49e0; __utmc=77520967 
Referer: http://abcdef.com/v2.swf\r\n\r\n 
Referer: http://abcdef.com/v2.swf\r\n 
Content-Type: application/x-amf\r\n 
Content-Length: 50\r\n 
\r\n 

這裏是Wireshark的捕獲的PHP代碼與info.txt用十六進制編輯器和所有的信息做出正確(即50個字節,確切的HEX內容)

// Get cookie 
$ch = curl_init('http://abcdef.com/'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
preg_match('/^Set-Cookie: (.*?);/m', curl_exec($ch), $m); 

// Read x-amf data 
$fileHandle = fopen("info.txt", "rb"); 
$postdata = stream_get_contents($fileHandle); 
fclose($fileHandle); 

// Send POST request to server 
$opts = array('http' => 
      array(
       'method' => 'POST', 
       'header' => " 
        User-Agent: Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0\r\n 
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n 
        Accept-Language: en-us,en;q=0.5\r\n 
        Accept-Encoding: gzip, deflate\r\n 
        Accept-Charset: UTF-8,*\r\n 
        Keep-Alive: 115\r\n 
        Cookie: ".$m[1]."\r\n 
        Connection: keep-alive\r\n 
        Referer: http://abcdef.com/v2.swf\r\n 
        Content-Type: application/x-amf\r\n 
        Content-Length: 50\r\n", 
       'content' => $postdata 
      ) 
     ); 
$context = stream_context_create($opts); 
$result = file_get_contents('http://abcdef.com/engine/', false, $context); 
print_r($result); 

結果是空白頁而不是來自服務器的響應。

+0

哪來的餅乾嗎? – trickwallett 2011-04-13 13:45:35

+0

哦...我認爲cookie雖然不是必要的,但是我只是添加了「Cookie:__utma = 77520967.190998754.1302600802.1302605710.130269308 ....」,並且空白空間的結果仍然相同,沒有響應 – markbse 2011-04-13 13:48:26

+0

給定了PHP會話cookie在那裏,在猜測你必須獲得一個有效的會話ID .. – trickwallett 2011-04-13 13:51:04

回答

1

解決方案示例:

// Get content of x-amf file (must read in binary mode) 
$fileHandle = fopen("info.txt", "rb"); 
$postdata = stream_get_contents($fileHandle); 
fclose($fileHandle); 

// Get cookie for CURL 
$ch = curl_init('http://abcdef.com/'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
preg_match('/^Set-Cookie: (.*?);/m', curl_exec($ch), $m); 

// Set headers for CURL (with cookie stored in $m) 
$header = array(
      "POST /engine/ HTTP/1.1", 
      "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523 Ubuntu/9.04 (jaunty) Firefox/3.0.10", 
      "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
      "Accept-Language: de, en-gb;q=0.9, en;q=0.8", 
      "Accept-Encoding: gzip", 
      "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7", 
      "Cache-Control: no-cache", 
      "Pragma: no-cache", 
      "Connection: close", 
      "Referer: http://abcdef.com/v2.swf", 
      "Content-Type: application/x-amf", 
      "Cookie: ".$m[1], 
      "Host: abcdef.com", 
      "Content-Length: 50", 
); 

// Set options for CURL 
$options = array(
    CURLOPT_HTTPHEADER   => $header, 
    CURLOPT_POST    => true, 
    CURLOPT_POSTFIELDS   => $postdata, 
    CURLOPT_FOLLOWLOCATION  =>true 
); 

// POST the CURL and enjoy the outcome :) 
$ch  = curl_init("http://abcdef.com"); 
curl_setopt_array($ch, $options); 
$content = curl_exec($ch); 
$err  = curl_errno($ch); 
$errmsg = curl_error($ch); 
$header = curl_getinfo($ch); 
curl_close($ch);