PHP https使用cURL发布XML数据

时间:2010-12-02 03:59:23

标签: php post curl https

我正在尝试使用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   Content-Type:text / xml; charset = UTF-8   内容长度:0日期:星期四,02年12月2日   2010 03:02:33 GMT

我确定文件类型正确,XML格式正确。 我已经尝试了urlencode字段,但它没有用。 还有什么我可能做错了?

3 个答案:

答案 0 :(得分:3)

我用

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

你需要用原始代码:

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。