<?php
//  Filename: 2SMS.php
//  Author:   Charlie Boisseau  <http://www.boisseau.co.uk>
//                               charlie at boisseau dot co dot uk
//  Modified: 2nd April 2004
//  Purpose:  This file contains the main function for sending the SMS
//            through the 2SMS network.  It also contains an easy to
//            use class for handling the XML response from the server.

//  Requires: This example needs libCURL to be installed and active.
//            libCURL is used to make the actuall http request.

//            This code is completely free, however if you are
//            using it, I'd apreciate a post card, email or even
//            a donation (details for all three are available at
//            <http://www.boisseau.co.uk/>.

function cd ($data) {
    
//  The Micro$oft system that 2SMS uses needs you to wrap any strings
    //  which contian special characters (@!+_-©':=) etc with a tag to make
    //  sure it is interpreted literally and not interpreted as an XML
    //  element.  This function simply does that for us! 

    
$text str_replace("<![CDATA["""$data);
    
$text str_replace("]]>"""$text);
    return 
$text;
}

class 
SimpleXML {

    
//  This is a class to handle all the XML output from the server.
    //  The $parser is a PHP XML parser object.  Given a set of 
    //  start, end and default element handlers, it will automatically
    //  itterate through the XML, storing each value it comes accross
    //  as an item in the $record array, with the field name as it's
    //  key.  For example an XML string <Userid>test</Userid> would
    //  appear in the array as $record['Userid'] = "test".

    
var $parser;
    var 
$current_field '';
    var 
$record;
    
    function 
SimpleXML ($data) {
    
        
//  This constructor takes the XML data as it's single parameter.
        //  We then set the element handlers and kick off the auto-parser.
    
        
$this->parser xml_parser_create();
        
xml_set_object($this->parser, &$this);
        
xml_set_element_handler($this->parser'startElement''endelement');
        
xml_set_default_handler($this->parser'getElement');
        
xml_parse($this->parser$data);
        
        
//  Now check for any parse errors, and exit with an error if we
        //  have.
        
        
$err xml_get_error_code($this->parser);
        
xml_parser_free($this->parser);
        if (
$err != XML_ERROR_NONE) die(xml_error_string($err));
    }
    
    
//  Here are the actuall element handlers that get called by the parser.

    //  The startElement handler sets the current field variable to whatever
    //  element we are currently parsing.
    
function startElement ($p$element) { $this->current_field $element;    }    
    
    
//  The endElement handler clears the current field variable, to make sure
    //  we don't over-write any item already in the array.    
    
function endElement ($p$element) { $this->current_field '';    }
    
    
//  In the default handler, getElement, we actually store the item in the
    //  $record array after checking we haven't been given an empty field.
    
function getElement ($p$text) {
        if (
trim($text) != '') {
            
$this->record[$this->current_field] .= $text;
        }
    }
}

function 
sendSMS($username$password$destination$message) {

    
//  This function does the dirty work.  It uses libCURL to open an HTTP
    //  session to the 2sms server, construct our request, and send it down.

    //  First initialise the http session, exit if there was an error.

    
$ch curl_init('http://www.2sms.com/xml/xml.axp');
    if (!
$ch) { die(sprintf('Error [%d]: %s'curl_errno($ch), curl_error($ch))); }
    
    
//  Now we manually construct an XML SMSRequest.  This could be updated
    //  at a later date to accommodate multiple messages, or multiple
    //  recipients.

    
$xmlMessage .= "<"."?xml version='1.0'?".">";
    
$xmlMessage .= "\n<SMSRequest xmlns='x-schema:http://www.2sms.com/xml/smsxml.xml'>";
    
$xmlMessage .= "\n    <Userid><![CDATA[$username]]></Userid>";
    
$xmlMessage .= "\n    <Password>$password</Password>";
    
$xmlMessage .= "\n    <RequestList>";
    
$xmlMessage .= "\n        <Request>sendMessage</Request>";
    
$xmlMessage .= "\n        <Message>";
    
$xmlMessage .= "\n            <Destination>$destination</Destination>";
    
$xmlMessage .= "\n            <Text>$message</Text>";
    
$xmlMessage .= "\n        </Message>";
    
$xmlMessage .= "\n    </RequestList>";
    
$xmlMessage .= "\n</SMSRequest>";

    
//  Now asscociate the POST data to the http session we are using.
    
curl_setopt($chCURLOPT_POST1);
    
curl_setopt($chCURLOPT_POSTFIELDS$xmlMessage);
    
    
//  Set the outgoing content-type header.
    
curl_setopt($chCURLOPT_HTTPHEADER, array('"Content-Type:", "text/xml"'));
    
    
//  Make sure we recieve the returned data.
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);

    
//  Now we actually perform the http request.  Exit if there was an error.
    
$result curl_exec($ch);
    if (!
$result) die(sprintf('Error [%d]: %s'curl_errno($ch), curl_error($ch)));

    
//  Close the http session.
    
curl_close($ch);
    
    
//  Return the response from the server.
    
return $result;
}    
?>