使用HTTP POST和PHP发送XML数据

我需要发送这个XML

<?xml version="1.0" encoding="UTF-8"?> <gate> <country>NO</country> <accessNumber>1900</accessNumber> <senderNumber>1900</senderNumber> <targetNumber>4792267523</targetNumber> <price>0</price> <sms> <content><![CDATA[This is a test æøå ÆØÅ]]></content> </sms> </gate> 

到一个SMS网关服务。 该服务侦听HTTP POST请求。 XML必须embedded到POST请求的BODY中。

我使用PHP和CodeIgniter框架,但我是一个PHP的总数n00b,所以理想情况下,我需要一个彻底的指导,但任何指针在正确的方向将不胜感激。

你可以使用cURL库发布数据: http : //www.php.net/curl

 $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_URL, "http://websiteURL"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=".$xmlcontent."&password=".$password."&etc=etc"); $content=curl_exec($ch); 

哪里postfield包含您需要发送的XML – 您将需要命名postfield API服务(Clickatell我猜)期望

另一个选项是file_get_contents()

 // $xml_str = your xml // $url = target url $post_data = array('xml' => $xml_str); $stream_options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n", 'content' => http_build_query($post_data))); $context = stream_context_create($stream_options); $response = file_get_contents($url, null, $context);