php post形式发送xml多种方法

发布时间:2021-01-12编辑:脚本学堂
有关php post方式发送xml数据的几种方法,curl与fsockopen二种方法,二个入门实例代码,需要的朋友参考下。

本节内容:
php以post形式发送xml的方法

例1,使用curl:
 

复制代码 代码示例:
$xml_data = <xml>...</xml>";
$url = 'http://www.xxxx.com';
$header[] = "Content-type: text/xml";//定义content-type为xml
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$response = curl_exec($ch);
if(curl_errno($ch))
{
    print curl_error($ch);
}
curl_close($ch);

例2,使用fsockopen:
 

复制代码 代码示例:
$fp = fsockopen($server_ip, 80);
fputs($fp, "POST $path HTTP/1.0rn");
fputs($fp, "Host: $serverrn");
fputs($fp, "Content-Type: text/xmlrn");
fputs($fp, "Content-Length: $contentLengthrn");
fputs($fp, "Connection: closern");
fputs($fp, "rn"); // all headers sent
fputs($fp, $xml_data);
$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
return $result;