views:

1469

answers:

2

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 framwork, but I'm a total PHP n00b, so ideally I'd need a thorough guide, but any pointers in the right direction is appreciated.

+6  A: 

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

dusoft
Yeah, I wish... I asked out IT-guy if he could install cURL, but there was no chance he could do it within reasonable time.
Frode
then, see this blog post: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
dusoft
There are also the powerful and very nice pecl_http extension and the various PEAR HTTP_* packages (easier for your IT guy to install).
GZipp
@dusoft - Thanks for the link to netevil.org
GZipp
+3  A: 

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);
GZipp
yeah, streams have been introduced in PHP 4.3, but are quite hidden for most of the users.
dusoft