Receive MO SMS Requests with XML over HTTP POST

Modified on Fri, 01 Feb 2013 09:23 by CM — Categorized as: PHP, Tutorials, XML API

<?php
//************************************************************
//* PSWinCom Gateway Receive MO SMS by HTTP POST API				 *
//* PHP Code sample 																				 *
//* Disclaimer: Code given for demonstration purposes only.  *
//************************************************************
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) 
    $postText = file_get_contents('php://input'); 

// Response documentation: http://wiki.pswin.com/Receive%20message%20response%20XML.ashx
echo "<?xml version=\"1.0\"?>
<!DOCTYPE MSGLST SYSTEM \"pswincom_receive_response.dtd\"> 
<MSGLST> 
  <MSG> 
    <ID>1</ID> 
    <STATUS>OK</STATUS> 
  </MSG> 
</MSGLST>";


// Using SimpleXML to parse incoming request.
// Refer to gateway documentation for info about each xml parameter.
$xml = new SimpleXMLElement($postText);
$sendernumber = $xml->MSG[0]->SND;
$receivernumber = $xml->MSG[0]->RCV;
$messagetext = $xml->MSG[0]->TEXT;


// Sending result as email for demonstration purposes, normally you would store to database
$to = "bjorn@pswin.com";
$subject = "PSWinCom MO SMS XMLHTTP2 with PHP";
 
$body = "Messaged was received at:" . $datetime=date('y-m-d H:i:s');
$body .= "\r\nRaw XML: $postText";
$body .= "\r\n";
$body .= "\r\nMessage was sent from mobile number: $sendernumber";
$body .= "\r\nMessage was received at access number: $receivernumber";
$body .= "\r\nMessage text was: $messagetext";
 
mail($to, $subject, $body)
?>