PHP https 使用 cURL 发布 XML 数据

发布于 2024-10-05 04:19:16 字数 1580 浏览 2 评论 0原文

我正在尝试使用 PHP 将带有 XML 数据的 HTTPS POST 请求发送到服务器。

发送到服务器的任何内容都需要身份验证,因此我将使用 cURL。

一些背景信息:XML数据是请求服务器将文件从特定URL上传到其本地存储。

使用此 API 的一条规则是我必须将每个请求的内容类型设置为 application/xml

这就是我所做的,但不起作用...

<?php
$fields = array(
'data'=>'<n1:asset xmlns:n1="http://.....com/"><title>AAAA</title><fileName>http://192.168.11.30:8080/xxx.html</fileName><description>AAAA_desc</description><fileType>HTML</fileType></n1:asset>'
);
$fields_string = "";
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');

$url = "https://192.168.11.41:8443/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "admin:12345678");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml', 'Content-length: '. strlen($fields_string)) );

$result = curl_exec( $ch );

curl_close($ch); 

echo $result;
?>

我预计会收到上传成功或上传失败的 XML 回复。 但我却收到此错误消息。

HTTP/1.1 415 不支持的媒体类型 服务器:Apache-Coyote/1.1 内容类型:text/xml;字符集=UTF-8 内容长度:0 日期:12 月 2 日星期四 2010 03:02:33 GMT

我确信文件类型是正确的,XML 格式是正确的。 我尝试过对字段进行 urlencode 但没有成功。 我还可能做错了什么?

I'm trying to send a HTTPS POST request with XML data to a server using PHP.

Anything sends to the server requires authentication therefore I'll use cURL.

Some background info.: the XML data is to request the server to upload a file from a specific URL to its local storage.

One rule of using this API is I MUST set the content type for each request to application/xml.

This is what I've done but isn't working...

<?php
$fields = array(
'data'=>'<n1:asset xmlns:n1="http://.....com/"><title>AAAA</title><fileName>http://192.168.11.30:8080/xxx.html</fileName><description>AAAA_desc</description><fileType>HTML</fileType></n1:asset>'
);
$fields_string = "";
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');

$url = "https://192.168.11.41:8443/";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "admin:12345678");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml', 'Content-length: '. strlen($fields_string)) );

$result = curl_exec( $ch );

curl_close($ch); 

echo $result;
?>

I am expected to get an XML reply of either upload successful or upload failed.
But instead I am getting this error message.

HTTP/1.1 415 Unsupported Media Type
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=UTF-8
Content-Length: 0 Date: Thu, 02 Dec
2010 03:02:33 GMT

I'm sure the file type is correct, the XML format is correct.
I've tried urlencode the fields but it didn't work.
What else I might have done wrong?

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

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

发布评论

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

评论(3

心的位置 2024-10-12 04:19:17

也许您可以尝试使用 text/xml 而不是 application/xml?

Perhaps you could try text/xml instead of application/xml?

微暖i 2024-10-12 04:19:17

您必须在原始代码中执行以下操作:

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields['data']);

或者

curl_setopt($ch, CURLOPT_POSTFIELDS, '<n1:asset xmlns:n1="http://.....com/"><title>AAAA</title><fileName>http://192.168.11.30:8080/xxx.html</fileName><description>AAAA_desc</description><fileType>HTML</fileType></n1:asset>');

我的意思是您必须直接发送 XML。

You have to do, in you original code:

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields['data']);

or

curl_setopt($ch, CURLOPT_POSTFIELDS, '<n1:asset xmlns:n1="http://.....com/"><title>AAAA</title><fileName>http://192.168.11.30:8080/xxx.html</fileName><description>AAAA_desc</description><fileType>HTML</fileType></n1:asset>');

I mean that you have to send the XML directly.

南汐寒笙箫 2024-10-12 04:19:16

我解决了

$xml = $this->getXml();
$url = $this->getUrl();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                       'Content-type: application/xml', 
                                       'Content-length: ' . strlen($xml)
                                     ));
$output = curl_exec($ch);
curl_close($ch);

I solve with

$xml = $this->getXml();
$url = $this->getUrl();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                       'Content-type: application/xml', 
                                       'Content-length: ' . strlen($xml)
                                     ));
$output = curl_exec($ch);
curl_close($ch);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文