HTTP Post headers, received differently from what's being sent
-
13-09-2019 - |
Question
HI I'm tring to send some headers in my PHP script such as
$headers[] = "BATCH_TYPE: XML_SINGLE";
$headers[] = "VENDOR_ID: 56309";
But they are being received as:
Batch-Type Vendor-ID
..not as they were intended or required - which is causing me problems.
Anyone know why or how to sort?
Thanks,
<?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) ;
// 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;
}
$request_file = "./post_this.xml";
$fh = fopen($request_file, 'r');
$xml_data = fread($fh, filesize($request_file));
fclose($fh);
$url = 'http://www.xhaus.com/headers';
$headers = array();
$headers[] = "Expect:";
$headers[] = "Accept: text/xml";
$headers[] = "BATCH_TYPE: XML_SINGLE";
$headers[] = "BATCH_COUNT: 1";
$headers[] = "VENDOR_ID: 54367";
$Response = httpsPost($url, $xml_data, $headers);
echo $Response;
?>
Solution 4
After a week of battling with the company of this external server, they were in fact giving me the wrong headers - D'oh!
OTHER TIPS
How are you checking these headers ? I've just tried myself with the following code:-
<?php
header("BATCH_TYPE: XML_SINGLE");
And got back the following:-
HTTP/1.1 200 OK
Date: Wed, 10 Jun 2009 08:56:54 GMT
Server: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.1 with Suhosin-Patch
X-Powered-By: PHP/5.2.6-3ubuntu4.1
BATCH_TYPE: XML_SINGLE
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html
Use fsockopen() and write/read manually anything you need. I'm not sure if your implementation of CURL or smth. else like proxy doesn't change your headers. If you want to do something good for sure - just do it yourself ;). Actually it's so easy to create HTTP-request and write it to opened socket...
$req = "POST $url HTTP/1.0\n";
$headers[] = 'VENDOR_ID: 1234';
$headers[] = 'MY_OTHER_HEADER: xxxxx';
$req .= implode("\n", $headers);
$req .= "\n\n" . $request_body;
$sock = fsockopen($host, 80, $errno, $errstr, $timeout);
fwrite($sock, $req, strlen($req));
$return = '';
do
{
$return .= fread($sock, 512);
} while (!feof($sock));
fclose($sock);
Not sure, but it seems to me, that smth. like this has already been done somewhere in PEAR...
Change the following code:
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
To this:
foreach($headers as $header)
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);