USSD menu example code (PHP)
<?php
//Change the trigger string to trigger on custom text, the text is case insensitive.
$ussd_trigger_str = 'start ussd';
//If the get contains the "model" parameter start a new ussd session
if( array_key_exists( 'model', $_GET ) )
{
//START USSD SESSION
$dest_MSISDN = $_GET['originator'];
$msg_content = $_GET['msg'];
//The compare is case insensitive
if( strcasecmp( $ussd_trigger_str, $msg_content ) == 0 )
{
$to = $dest_MSISDN;
$message = 'Test';
define( 'USSD_APPLICATION_CODE', 'txtNation_USSD_prompt' );
define( 'USSD_SERVER' ,'195.84.167.36' );
define( 'USSD_PORT' ,'5000' );
define( 'USSD_URL','/umsprot' );
$sessionid = uniqid();
//Create xml to start the USSD session
$xmlstr = "<?xml version='1.0' ?>\n". "<umsprot></umsprot>";
$root = new SimpleXMLElement( $xmlstr );
$root->addAttribute( "version" , 1 );
$mehodElement = $root->addChild( 'ao_start_req' );
$mehodElement->addAttribute( 'msisdn' , $to );
$mehodElement->addAttribute( 'appcode' ,
USSD_APPLICATION_CODE );
$mehodElement->addAttribute( 'sessionid' , $sessionid );
$messageElement = $mehodElement->addChild( 'data',
$message );
$messageElement->addAttribute( 'name' , 'message');
$xml_data = $root->asXML();
$url = 'http://' . USSD_SERVER . ':' . USSD_PORT . USSD_URL;
//Trigger the USSD session with http request.
$curl = curl_init( $url );
$headers = array(
"POST ".USSD_URL." HTTP/1.0",
"Content-type: application/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Content-length: ".strlen($xml_data)
);
$setOk = curl_setopt( $curl, CURLOPT_TIMEOUT, 60);
$setOk = curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$setOk = curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers);
$setOk = curl_setopt( $curl, CURLOPT_POST, true );
$setOk = curl_setopt( $curl, CURLOPT_POSTFIELDS, $xml_data );
$sent = curl_exec( $curl );
if( ! $sent )
{
$error = curl_error( $curl );
}
}
}
else
{
curl_close( $curl );
exit();
//Handel request from the Menu server
$questions_array = array(
array( 'msg' => "Welcome to the USSD demo!\n\nPlease enter your
name:", 'req_type'=>'prompt_req' ),
array( 'msg' => "%s, which is your favorite movie?\n\n1. Independence
Day\n2. Top Gun 2\n3. The Thing", 'req_type'=> 'prompt_req' ),
array( 'msg' => "%s, which is your favorite band?\n\n1. The Rolling
Stones\n2. The XX\n3. Joy Division", 'req_type'=> 'prompt_req' ),
array( 'msg' => "%s, which is your favorite game?\n\n1. Minecraft\n2.
Star Craft 2\n3. Battlefield 3", 'req_type' => 'prompt_req'),
array( 'msg' => "Thanks for answering our survey, have a nice day!",
'req_type' => 'display_req')
);
$requestXML = simplexml_load_string( $HTTP_RAW_POST_DATA );
$functionNode
$functionName
= $requestXML->xpath( '/umsprot/*[1]' );
= $functionNode[0]->getName();
$functionNode_attributes = $functionNode[0]->attributes();
$seesionId = $functionNode_attributes['sessionid'];
//Different functions to take action on
switch( $functionName )
{
case "open_session":
$message = $questions_array[0];
$xmlstr = "<?xml version='1.0' ?>\n".
"<umsprot></umsprot>";
$root = new SimpleXMLElement( $xmlstr );
$root->addAttribute( "version" , 1 );
$mehodElement = $root->addChild( $message['req_type'],
$message['msg'] );
$mehodElement->addAttribute( 'reqid' , '0' );
$mehodElement->addAttribute( 'sessionid' , $seesionId );
echo $root->asXML();
exit();
break;
case "prompt_rsp"://The subscriber have replyed
$req_id = intval( $functionNode_attributes['reqid'] );
//Load session data
$sessionFileName = '/tmp/ussd_sess_' . $seesionId;
if( file_exists( $sessionFileName ) )
{
$localSessionData =
unserialize(file_get_contents( $sessionFileName ));
}
else if( $req_id == 0 )
{
//Create new local session file
$localSessionData = array( 'subscriber_name' =>
sprintf( '%s', $functionNode[0]) );
file_put_contents( $sessionFileName,
serialize( $localSessionData ) );
}
$next_req_id = $req_id + 1;
if( array_key_exists( $next_req_id ,$questions_array) )
{
$message = $questions_array[$next_req_id];
"<umsprot></umsprot>";
$xmlstr = "<?xml version='1.0' ?>\n".
$root = new SimpleXMLElement( $xmlstr );
$root->addAttribute( "version" , 1 );
$mehodElement = $root>addChild( $message['req_type'], sprintf ( $message['msg'],
$localSessionData['subscriber_name'] ) );
$mehodElement->addAttribute( 'reqid' ,
$next_req_id );
$mehodElement->addAttribute( 'sessionid' ,
$seesionId );
}
echo $root->asXML();
exit();
break;
case "abort_session_req":
}
?>
$sessionFileName = '/tmp/ussd_sess_' . $seesionId;
unlink($sessionFileName);
break;