HTTP Post标头的收据与发送的不同

时间:2009-06-10 08:23:31

标签: php http-headers http-post

我想在我的PHP脚本中发送一些标题,例如

$headers[] = "BATCH_TYPE: XML_SINGLE"; 
$headers[] = "VENDOR_ID: 56309";

但他们被收到了:

间歇式 厂商ID

..不是因为它们的意图或要求 - 这导致了我的问题。

任何人都知道为什么或如何排序?

谢谢,

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

?>

4 个答案:

答案 0 :(得分:0)

你是如何检查这些标题的?我刚刚尝试使用以下代码: -

<?php

header("BATCH_TYPE: XML_SINGLE");

并取回以下内容: -

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

答案 1 :(得分:0)

使用fsockopen()并手动编写/读取您需要的任何内容。我不确定你是否实现了CURL或smth。其他像代理不会改变你的标题。如果你想做一些好的事情 - 只需自己动手;)。实际上,创建HTTP请求并将其写入已打开的套接字是如此容易......

$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);

不确定,但在我看来,那个smth。像这样已经在PEAR的某个地方完成了......

答案 2 :(得分:0)

更改以下代码:

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

对此:

foreach($headers as $header)
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

答案 3 :(得分:0)

经过一周与这个外部服务器公司的斗争,他们实际上给了我错误的标题 - D'哦!