0

这是来自 WireShark 的转储:

POST /drm/drm_production_v2.php HTTP/1.1

content-length: 278

content-type: text/xml

User-Agent: UserAgent 1.0

Connection: Keep-Alive

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=

host: www.example.com



<methodCall>
  <methodName>aMethod</methodName>
  <params>
    <param>
      <value>
        <base64>dXNlcm5hbWU6cGFzc3dvcmQ=</base64>
      </value>
    </param>
    <param>
      <value>
        <struct/>
      </value>
    </param>
  </params>
</methodCall>

我将 xml 保存到一个单独的文件中。这就是我正在做的事情:

<?php

function httpsPost($Url, $xml_data, $headers)
{
   // Initialisation
   $ch=curl_init();
   // Set parameters
   curl_setopt($ch, CURLOPT_FORBID_REUSE, 1); 
   curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_URL, $Url);
   // Return a variable instead of posting it directly
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_USERPWD,"username:password");

   // Activate the POST method
   curl_setopt($ch, CURLOPT_POST, 1) ;
   curl_setopt($ch,CURLOPT_USERAGENT,"UserAgent 1.0"); 
   // Request
   curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
   curl_setopt($ch, CURLOPT_TIMEOUT, 999);

   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

   // execute the connexion
   $result = curl_exec($ch);
   // Close it
   curl_close($ch);
   return $result;
}
$str='username:password';
$auth=base64_encode($str);
$request_file = "./request.xml"; 
$fh = fopen($request_file, 'r'); 
$filesize=filesize($request_file);
echo $filesize;
$xml_data = fread($fh,$filesize);

fclose($fh);    

$url = 'http://www.example.com';

$header = array();
$header[] = "POST /drm/drm_production_v2.php HTTP/1.1";
$header[] = "Content-type: text/xml";
$header[] = "content-length: ".$filesize. "\r\n";
$header[] = "User-Agent: UserAgent 1.0";
$header[] = "Connection: Keep-Alive";
$header[] = "Authorization: Basic ".$auth;
$header[] = "host: www.example.com";


$Response = httpsPost($url, $xml_data, $header);

echo $Response;

?>

它从服务器返回一个“错误请求”。有什么建议么?

4

3 回答 3

2

我的第一个猜测是内容长度标头后的额外“\r\n”使服务器认为帖子内容从那里开始。我还将“content-length”、“Content-type”和“host”更改为“Content-Length”、“Contnet-Type”和“Host”,以防万一。

编辑:那个,还有罗纳德·布曼的回答。

于 2009-12-22T08:27:13.110 回答
2

我认为你的论点

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

是不正确的。postfields 选项应该是 URL 编码的名称/值对。从文档:

" 这可以作为 urlencoded 字符串(如 'para1=val1¶2=val2&...'设置为多部分/表单数据“

http://php.net/manual/en/function.curl-setopt.php

于 2009-12-22T08:31:41.220 回答
0

pcap2curl工具允许将 HTTP 请求的 pcap 文件转换为 cURL

但是,如果您想从浏览器重放一些 Web 请求,那么您可以不使用 Wireshark,而是在浏览器中进入 Web 开发人员模式。然后您转到网络请求视图并右键单击感兴趣的请求,然后在大多数现代浏览器中,有一个复制为 cURL的选项,然后您可以将生成的命令粘贴到终端并重新运行捕获的命令使用该curl工具查看是否合适。某些浏览器(例如 Mozilla)还提供从浏览器中编辑和重新发送的选项。

于 2018-01-22T15:00:09.170 回答