views:

358

answers:

2

Here is the dump from 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>

I have the xml saved into a seperate file. Here's what I am doing:

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

?>

It returns a 'Bad Request' from the server. Any suggestions?

+1  A: 

My first guess is that the extra "\r\n" after the content-length header makes the server think that the post content starts there. I'd also change "content-length", "Content-type", and "host" to "Content-Length", "Contnet-Type", and "Host", just in case.

Edit: That, and Ronald Bouman's answer.

GZipp
Sorry, Roland, to have gotten your name wrong.
GZipp
+1  A: 

I think your argument to

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

is not correct. The postfields option should be URL encoded name/value pairs. From the docs:

" This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data "

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

Roland Bouman