php如何post XML到指定服务器
1个回答
2014-11-11
展开全部
以下是一个编写好的post XML 的类:
<?php
class xmlSender {
/**
* 构造器
* 校验 cURL 是不是可用
*/
function xmlSender()
{
if ( !extension_loaded('curl') ) {
trigger_error("You need cURL loaded to use this class", E_USER_ERROR);
}
}
/**
* 使用了cURL库发送 xml 内容
*/
function send( $str_xml, $str_url, $str_page, $boo_ssl = false )
{
$str_header = "POST " . $str_page . " HTTP/1.0 \r\n";
$str_header .= "MIME-Version: 1.0 \r\n";
$str_header .= "Content-type: application/PTI26 \r\n";
$str_header .= "Content-length: " . strlen($str_xml) . " \r\n";
$str_header .= "Content-transfer-encoding: text \r\n";
$str_header .= "Request-number: 1 \r\n";
$str_header .= "Document-type: Response\r\n";
$str_header .= "Interface-Version: Site 1.0 \r\n";
$str_header .= "Connection: close \r\n\r\n";
$str_header .= $str_xml;
$res_curl = curl_init();
curl_setopt($res_curl, CURLOPT_URL, $str_url);
curl_setopt($res_curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($res_curl, CURLOPT_TIMEOUT, 30);
curl_setopt($res_curl, CURLOPT_CUSTOMREQUEST, $str_header);
curl_setopt($res_curl, CURLOPT_FOLLOWLOCATION, 1);
if ( $boo_ssl ) {
curl_setopt($res_curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($res_curl, CURLOPT_SSL_VERIFYPEER, false);
}
$str_data = curl_exec($res_curl);
if ( curl_errno($res_curl) ) {
trigger_error(curl_error($res_curl), E_USER_ERROR);
} else {
curl_close($res_curl);
}
return $str_data;
}
}
$str_xml = '<xxx>blablabla</xxx><yyy>blebleble</yyy><zzzz>bliblibli</zzz>';
$o = new xmlSender;
print_r($o->send($str_xml, "https://endereco.com.br/", "/yyy/x.x.x/", true));
?>
=========================================
这几天我正在研究cURL,这个库正好能够完成你的需要,具体代码的编写需要示例数据与网站才能测试,以下是post的示例:
<?php
$url = 'http://www.example.com/submit.php';
// The submitted form data, encoded as query-string-style
// name-value pairs
$body = 'monkey=uncle&rhino=aunt';
$c = curl_init($url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $body);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($c);
curl_close($c);
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询