USSD Sample Code

Sample USSD Menu Code

Below is a sample 2-level USSD menu in PHP. This can be extended in the same format to an unlimited menu level within the session timeout period permitted on the USSDC.

<?php

/*
* Read HTTP GET values
*/

$session_msisdn = $_GET['session_msisdn'];
$session_operation = $_GET['session_operation']; /* begin, continue, end */
$session_type = $_GET['session_type']; /* 1, 2, 3, 4 */
$session_msg = trim($_GET['session_msg']);
$session_id = $_GET['session_id'];
$session_from = $_GET['session_from']; /* shortcode */

/*
* Depending on the USSD operation category, send a corresponding 
* response message back to the subscriber.
*/

if (strcmp($session_operation, "begin") == 0) {
    $output['session_operation'] = "continue";
    $output['session_type'] = 1;
    $output['session_id'] = $session_id;
    $output['session_msg'] = "Welcome to eBanking.\nPlease select your payment channel.\n1) Card\n2) Bank Account\n3) Wallet";
} elseif (strcmp($session_operation, "continue") == 0) {
    $output['session_operation'] = "end";
    $output['session_type'] = 4;
    $output['session_id'] = $session_id;

    /*
    * Below is the business logic based on the response of the user.
    * You can pass this to your database, internal system etc.
    */

    switch ($session_msg) {
        case 1:
            $channel = "Card";

            break;
        case 2:
            $channel = "Bank Account";

            break;
        case 3:
            $channel = "Wallet";

            break;
        default:
            $channel = "NONE";
    }

    $output['session_msg'] = "You selected -$channel-. Have a great day.";
} else {
    $output['session_operation'] = "end";
    $output['session_type'] = 4;
    $output['session_id'] = $session_id;
    $output['session_msg'] = "Thank you.";
}

echo json_encode($output); /* this JSON output goes to the USSDC */

exit;

Last updated

Was this helpful?