本文实例讲述了php以post形式发送xml的方法。分享给大家供大家参考。具体方法如下:
方法一,使用curl:
$xml_data = ...";<br />$url = 'http://www.xxxx.com';<br />$header[] = "Content-type: text/xml";//定义content-type为xml<br />curl_setopt($ch, CURLOPT_URL, $url);<br />curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />curl_setopt($ch, CURLOPT_HTTPHEADER, $header);<br />curl_setopt($ch, CURLOPT_POST, 1);<br />curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);<br />$response = curl_exec($ch);<br />if(curl_errno($ch))<br />{<br /> print curl_error($ch);<br />}<br />curl_close($ch);
方法二,使用fsockopen:
$fp = fsockopen($server_i<div style="color:transparent">!本文来源gaodai.ma#com搞#代!码(网</div><em>搞gaodaima代码</em>p, 80);<br />fputs($fp, "POST $path HTTP/1.0\r\n");<br />fputs($fp, "Host: $server\r\n");<br />fputs($fp, "Content-Type: text/xml\r\n");<br />fputs($fp, "Content-Length: $contentLength\r\n");<br />fputs($fp, "Connection: close\r\n");<br />fputs($fp, "\r\n"); // all headers sent<br />fputs($fp, $xml_data);<br />$result = '';<br />while (!feof($fp)) {<br />$result .= fgets($fp, 128);<br />}<br />return $result;
希望本文所述对大家的PHP程序设计有所帮助。