PHP https post XML数据与cURL [英] PHP https post XML data with cURL

查看:135
本文介绍了PHP https post XML数据与cURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

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



我希望得到一个XML回复成功上传或上传失败。

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不支持的媒体类型
服务器:Apache- Coyote / 1.1
Content-Type:text / xml; charset = UTF-8
Content-Length:0 Date:Thu,02 Dec
2010 03:02:33 GMT

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

我确定文件类型是正确的,XML格式是正确的。
我试过urlencode的字段,但它没有工作。
我可能做错了什么?

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?

推荐答案

我解决了

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

这篇关于PHP https post XML数据与cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆