问题描述
我试图重现使用PHP从WireShark捕获的POST请求。这个POST请求是由Flash(.swf)对象发送的,因此在配置头部时有点复杂。
它不会打印出任何东西,所以一定有一些错误的PHP代码,我看不到。
这是什么WireShark捕获:
POST / engine / HTTP / 1.1 \r\\\
主机:abcdef.com\r\\\
User-Agent:Mozilla / 5.0 X11; Linux i686; rv:2.0)Gecko / 20100101 Firefox / 4.0\r\\\
Accept:text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8\r\\\
Accept-Language:en-us,en; q = 0.5\r\\\
Accept-Encoding:gzip,deflate\r\\\
Accept-Charset:UTF-8,* \r\\\
Keep-Alive:115 \r\\\
连接:keep-alive\r\\\
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\\\
\r\\\
Referer:http://abcdef.com/v2.swf\\ \\ r \\\
Content-Type:application / x-amf\r\\\
Content-Length:50 \r\\\
\r\\\
这里是使用info.txt的PHP代码是由HEX编辑器和所有信息是正确的(即50字节,由WireShark捕获的精确HEX内容)
//获取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);
//读取x-amf数据
$ fileHandle = fopen(info.txt,rb);
$ postdata = stream_get_contents($ fileHandle);
fclose($ fileHandle);
//发送POST请求到服务器
$ opts = array('http'=>
array(
'method'=>'POST'
'header'=>
User-Agent:Mozilla / 5.0(X11; Linux i686; rv:2.0)Gecko / 20100101 Firefox / 4.0 \r\\\
接受: text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8\r\\\
Accept-Language:en-us,en; q = 0.5 \\ r \\\
Accept-Encoding:gzip,deflate\r\\\
Accept-Charset:UTF-8,* \r\\\
Keep-Alive:115 \\ \\ r \ n
Cookie:。$ m [1]。\r\\\
连接:keep-alive\r\\\
引用:http: /abcdef.com/v2.swf\r\\\
Content-Type:application / x-amf\r\\\
Content-Length:50 \r\\\
,
'content'=> $ postdata
)
);
$ context = stream_context_create($ opts);
$ result = file_get_contents('http://abcdef.com/engine/',false,$ context);
print_r($ result);
结果是一个空白页而不是来自服务器的响应。
//获取x- amf文件(必须以二进制模式读取)
$ fileHandle = fopen(info.txt,rb);
$ postdata = stream_get_contents($ fileHandle);
fclose($ fileHandle);
//为CURL获取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);
//设置CURL的头文件(cookie存储在$ 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: html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8,
Accept-Language:de,en-gb; q = 0.9,en; ,
Accept-Encoding:gzip,
Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.7控制:no-cache,
Pragma:no-cache,
Connection:close,
Referer:http://abcdef.com/v2.swf b $ bContent-Type:application / x-amf,
Cookie:。$ m [1],
Host:abcdef.com,
Content-Length :50,
);
//设置CURL的选项
$ options = array(
CURLOPT_HTTPHEADER => $ header,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS = > $ postdata,
CURLOPT_FOLLOWLOCATION => true
);
// POST CURL并享受结果:)
$ 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);
I'm trying to reproduce a POST request that was captured from WireShark using PHP. This POST request was sent by a Flash (.swf) object, so it's a little bit complicated in configuring the header.
It does not print out anything in the end, so there must be something wrong with PHP code that I could not see.
Here is what WireShark captured:
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
Here is the PHP code with info.txt was made by a HEX editor and all info are correct (i.e. 50 bytes, exact HEX content captured by WireShark)
// 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);
The result is a blank page instead of response from server.
Example Solution:
// 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 );
这篇关于使用x-amf(Flash)请求标头发送POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!