Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending XML data using HTTP POST with PHP

Tags:

I need to send this 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> 

to a SMS gateway service. The service listens for HTTP POST requests. The XML must be embedded in the BODY of the POST request.

I'm using PHP and the CodeIgniter framework, but I'm a total PHP n00b, so ideally I'd need a thorough guide, but any pointers in the right direction would be appreciated.

like image 429
Frode Avatar asked Nov 02 '09 12:11

Frode


2 Answers

you can use cURL library for posting data: 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); 

where postfield contains XML you need to send - you will need to name the postfield the API service (Clickatell I guess) expects

like image 118
dusoft Avatar answered Oct 01 '22 17:10

dusoft


Another option would be 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); 
like image 37
GZipp Avatar answered Oct 01 '22 16:10

GZipp