views:

363

answers:

3

Hi I'm currently trying to post an xml file to an external server, but I'm getting a response back that the headers are incorrect. The server I'm posting requires some headers and I'm wondering if they're in the correct format or if there are any other "standard" headers that need to be included?

My code is:

<?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");
   // Active the POST method
   curl_setopt($ch, CURLOPT_POST, 1) ;
   // Request
   curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
   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;
}

$headers[0]="BATCH_TYPE: XML_SINGLE"; 
$headers[1]="BATCH_COUNT: 1"; 
$headers[2]="VENDOR_ID: 53906";

$request_file = "./post_this.xml"; 
$fh = fopen($request_file, 'r'); 
$xml_data = fread($fh, filesize($request_file)); 
fclose($fh);  

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

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

echo $Response;

?>
A: 

try

curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); 
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Accept: text/xml"));

or use that

$header[] = "Host: ".$host; 
$header[] = "MIME-Version: 1.0"; 
$header[] = "Accept: text/xml";
$header[] = "Content-length: ".strlen($xmlRequest);
$header[] = "Cache-Control: no-cache";
$header[] = "Connection: close \r\n";
$header[] = $xmlRequest; // Contains the XML request

link maybe help with example: http://www.nabble.com/Problems-posting-xml-file-with-curl-and-php-td16129807.html

Haim Evgi
Are you saying to put request in: $header[] = $xml_data;rather than:curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
thegunner
Hi, What about the headers that external server requires?:$headers[0]="BATCH_TYPE: XML_SINGLE";$headers[1]="BATCH_COUNT: 1";$headers[2]="VENDOR_ID: 53906";Thanks,
thegunner
ok that's not working....
thegunner
sorry, i hope you found the solution.
Haim Evgi
I've been told that the problem may be that the headers are being received differently from what's being sent e.g.:VENDOR_ID is being received as Vendor-Id\nBATCH-TYPE is being received as Batch-Type..\nAnyone know how to sort that?
thegunner
A: 

I've done a bunch of research on this one, but I don't have any definitive answer. I'll go out on a limb and say that I think the problem is that your headers are using underscores instead of dashes. I can't think of any standard HTTP header that uses underscores.

This is perfectly legal per the HTTP 1.1 specification. According to RFC 2616:

message-header = field-name ":" [ field-value ]
field-name     = token
token          = 1*<any CHAR except CTLs or separators>
separators     = "(" | ")" | "<" | ">" | "@"
                  | "," | ";" | ":" | "\" | <">
                  | "/" | "[" | "]" | "?" | "="
                  | "{" | "}" | SP | HT

If it's an option, I suggest changing the server to expect Batch-Type, Batch-Count and Vendor-Id.

If Curl is changing your header names behind your back, you should report it as a bug.

Steve Madsen
Hi Steve, I totally agree with you - I think the people I'm posting to are making a mistake somewhere. From the code above it does look correct doesnt it? There's no obvious glaring error of why this post may not work is there?
thegunner
I just threw together a quick test script. PHP's curl functions are not changing the headers. They are being sent as-is, however, it's very possible the server-side environment is replacing _ with - and "fixing" the case. In PHP, if you try to get the headers in $_SERVER, they are up-cased with an HTTP_ prefix and - is converted to _. The original formatting is lost.
Steve Madsen
yeah that's right case doesn;t matter in headers it's in the spec for them...and _ does changed also.... just gonna accept that there isn't a prob with my headers...it's gotta be something else or something at their end. thanks for help.
thegunner
A: 

If I use a local script as the script being posted to, the output is correct.

The local script simply consists of

<?php
  phpinfo();
?>

So, I guess the problem is not with your code but with the display at http://www.xhaus.com/headers. That site seems to replace underscores with dashes for display. Why use that url for displaying the results and not your own phpinfo-script?

NineBerry