cURL, WireShark.

2 Cevap php

İşte WireShark gelen dökümü:

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>

Ben ayrı bir dosyaya kaydedilir xml. İşte ben yapıyorum ne:

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

?>

Bu sunucudan bir 'Bad Request' döndürür. Herhangi bir öneriniz?

2 Cevap

Benim ilk tahminim ekstra "\ r \ n" içerik-uzunluk başlığı yapar, sonra sunucu yazılan içerik orada başladığını düşünüyorum. Ben de "içerik-uzunluk", "Content-type" değiştirmek, ve istiyorum "ana" her ihtimale karşı "Content-Length", "contnet-Type" ve "Host", için.

Edit: Yani, ve Ronald Bouman cevabı.

Ben senin argüman düşünüyorum

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

doğru değil. PostFields seçenek, URL kodlanmış ad / değer çiftleri olmalıdır. Dokümanlar:

" 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 "

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