PHP - HTTP Post - 错误的标头

发布于 2024-07-22 17:46:52 字数 1313 浏览 2 评论 0原文

您好,我目前正在尝试将 xml 文件发布到外部服务器,但我收到的响应表明标头不正确。 我发布的服务器需要一些标头,我想知道它们的格式是否正确,或者是否需要包含任何其他“标准”标头?

我的代码是:

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

?>

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;

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一影成城 2024-07-29 17:46:52

我对此进行了大量研究,但没有任何明确的答案。 我会大胆地说,我认为问题在于您的标题使用下划线而不是破折号。 我想不出任何使用下划线的标准 HTTP 标头。

根据 HTTP 1.1 规范,这是完全合法的。 根据 RFC 2616:

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

如果可以选择,我建议将服务器更改为期望 Batch-Type、Batch-Count 和 Vendor-Id。

如果 Curl 在背后更改您的标头名称,您应该将其报告为错误

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.

我ぃ本無心為│何有愛 2024-07-29 17:46:52

尝试

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

或使用该

$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 

链接可能有助于示例:
http://www.nabble。 com/Problems-posting-xml-file-with-curl-and-php-td16129807.html

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

幽蝶幻影 2024-07-29 17:46:52

如果我使用本地脚本作为发布到的脚本,则输出是正确的。

本地脚本仅包含

<?php
  phpinfo();
?>

以下内容因此,我想问题不在于您的代码,而在于 http:// 的显示www.xhaus.com/headers。 该网站似乎用破折号替换下划线进行显示。 为什么使用该 url 来显示结果而不是您自己的 phpinfo 脚本?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文