PHP - HTTP Post - 错误的标头
您好,我目前正在尝试将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对此进行了大量研究,但没有任何明确的答案。 我会大胆地说,我认为问题在于您的标题使用下划线而不是破折号。 我想不出任何使用下划线的标准 HTTP 标头。
根据 HTTP 1.1 规范,这是完全合法的。 根据 RFC 2616:
如果可以选择,我建议将服务器更改为期望 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:
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.
尝试
或使用该
链接可能有助于示例:
http://www.nabble。 com/Problems-posting-xml-file-with-curl-and-php-td16129807.html
try
or use that
link maybe help with example:
http://www.nabble.com/Problems-posting-xml-file-with-curl-and-php-td16129807.html
如果我使用本地脚本作为发布到的脚本,则输出是正确的。
本地脚本仅包含
以下内容因此,我想问题不在于您的代码,而在于 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
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?