Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Created July 13, 2016 09:16
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 38 You must be signed in to fork a gist
  • Save abel-masila/493853c2c6d68d1cb1f0062545c9fb66 to your computer and use it in GitHub Desktop.
Save abel-masila/493853c2c6d68d1cb1f0062545c9fb66 to your computer and use it in GitHub Desktop.
A simple USSD registration application written in PHP
<?php
/* Simple sample USSD registration application
* USSD gateway that is being used is Africa's Talking USSD gateway
*/
// Print the response as plain text so that the gateway can read it
header('Content-type: text/plain');
/* local db configuration */
$dsn = 'mysql:dbname=dbname;host=127.0.0.1;'; //database name
$user = 'your user'; // your mysql user
$password = 'your password'; // your mysql password
// Create a PDO instance that will allow you to access your database
try {
$dbh = new PDO($dsn, $user, $password);
}
catch(PDOException $e) {
//var_dump($e);
echo("PDO error occurred");
}
catch(Exception $e) {
//var_dump($e);
echo("Error occurred");
}
// Get the parameters provided by Africa's Talking USSD gateway
$phone = $_GET['phoneNumber'];
$session_id = $_GET['sessionId'];
$service_code = $_GET['serviceCode'];
$ussd_string= $_GET['text'];
//set default level to zero
$level = 0;
/* Split text input based on asteriks(*)
* Africa's talking appends asteriks for after every menu level or input
* One needs to split the response from Africa's Talking in order to determine
* the menu level and input for each level
* */
$ussd_string_exploded = explode ("*",$ussd_string);
// Get menu level from ussd_string reply
$level = count($ussd_string_exploded);
if($level == 1 or $level == 0){
display_menu(); // show the home/first menu
}
if ($level > 1)
{
if ($ussd_string_exploded[0] == "1")
{
// If user selected 1 send them to the registration menu
register($ussd_string_exploded,$phone, $dbh);
}
else if ($ussd_string_exploded[0] == "2"){
//If user selected 2, send them to the about menu
about($ussd_string_exploded);
}
}
/* The ussd_proceed function appends CON to the USSD response your application gives.
* This informs Africa's Talking USSD gateway and consecuently Safaricom's
* USSD gateway that the USSD session is till in session or should still continue
* Use this when you want the application USSD session to continue
*/
function ussd_proceed($ussd_text){
echo "CON $ussd_text";
}
/* This ussd_stop function appends END to the USSD response your application gives.
* This informs Africa's Talking USSD gateway and consecuently Safaricom's
* USSD gateway that the USSD session should end.
* Use this when you to want the application session to terminate/end the application
*/
function ussd_stop($ussd_text){
echo "END $ussd_text";
}
//This is the home menu function
function display_menu()
{
$ussd_text = "1. Register \n 2. About \n"; // add \n so that the menu has new lines
ussd_proceed($ussd_text);
}
// Function that hanldles About menu
function about($ussd_text)
{
$ussd_text = "This is a sample registration application";
ussd_stop($ussd_text);
}
// Function that handles Registration menu
function register($details,$phone, $dbh){
if(count($details) == 2)
{
$ussd_text = "Please enter your Full Name and Email, each seperated by commas:";
ussd_proceed($ussd_text); // ask user to enter registration details
}
if(count($details)== 3)
{
if (empty($details[1])){
$ussd_text = "Sorry we do not accept blank values";
ussd_proceed($ussd_text);
} else {
$input = explode(",",$details[1]);//store input values in an array
$full_name = $input[0];//store full name
$email = $input[1];//store email
$phone_number =$phone;//store phone number
// build sql statement
$sth = $dbh->prepare("INSERT INTO customer (full_name, email, phone) VALUES('$full_name','$email','$phone_number')");
//execute insert query
$sth->execute();
if($sth->errorCode() == 0) {
$ussd_text = $full_name." your registration was successful. Your email is ".$email." and phone number is ".$phone_number;
ussd_proceed($ussd_text);
} else {
$errors = $sth->errorInfo();
}
}
}
}
# close the pdo connection
$dbh = null;
?>
@chepekaunda
Copy link

Thanks of this Code Abel,
In my application using Live gateway, the $ussd_string_exploded and $details arrays are getting reset each time the user enters a value. for example, if the user selects 1. Register , the $ussd_string_exploded [0] =>1 , the user enters Full Name and Email the $ussd_string_exploded array resets by replacing the the 1 entered at the beginning with the Full Name and Email so $ussd_string_exploded [0] => Full Name and Email and this disturbs the level count. please help where am i wrong?

@skilfulsidiq
Copy link

Thanks Abel, i don't understand where $level =0; is coming from. thanks

@nwikeforever
Copy link

This code is sure running but when I test on AT sandbox, I enter 1 and send, it runs and return to home menu again, same for wen I enter 2. What could be wrong, it does not move to the next level?

@saket-anand
Copy link

How can level is detected if whole string of ussd is not sent at once. Suppose first a user enter *888# we know it is first level. Now he/she enters 6 for registration. ? Now how to make 8886 ?

@Barojoel
Copy link

Thank you for your generosity.

@vimkaf
Copy link

vimkaf commented Aug 8, 2018

this code doesnt keep track of the level, it keeps resetting to 0, anytime the request is made

@zanmi1
Copy link

zanmi1 commented Aug 10, 2018

Is there anyone here who can help setup a USSD app to a telco gateway. We have established VPN connection, but somehow whenever someone dial our SC, they get a connection error. telco is telling us that our ussd app must connect to their gateway for them to see any incoming request. Any help would be greatly appreciated.

@bukombe
Copy link

bukombe commented Aug 19, 2018

@zanmi1 I can help you with connection to telco.email me @jeanpaulbukombe@gmail.com

@AwadziFelix
Copy link

hmmm

@kawesam
Copy link

kawesam commented Aug 14, 2019

@zanmi1 ,send an email to samlinncon@gmail.com , which Telko are you connecting to?

@Fahad565
Copy link

nice1!!!

@temamawelu
Copy link

dear how can i get
$phone = $_GET['phoneNumber'];
$ussd_string= $_GET['text'];
from xml soap

@felixmadukaku
Copy link

how can i code ussd for nigeria

@felixmadukaku
Copy link

{$ussd}*100#

  1. full name {name }
    2 state of origin

@kawesam
Copy link

kawesam commented Jan 7, 2020

@felixmadukaku . Do you have a connection to a telko for the ussd first?

@felixmadukaku
Copy link

felixmadukaku commented Jan 8, 2020 via email

@kawesam
Copy link

kawesam commented Jan 8, 2020 via email

@admin-hub
Copy link

Is that possible to open localhost/xampp server. How could i?

@nivedkrishnan
Copy link

how can add back button option

@Afrihunter
Copy link

I need someone that can help set up a Ussd App to integrate with AT's Api. Anyone offering service ?

@kawesam
Copy link

kawesam commented Aug 21, 2020

@Afrihunter, drop a line at samlinncon@gmail.com I do offer such services.

@kawesam
Copy link

kawesam commented Aug 21, 2020 via email

@JoelKiprono
Copy link

great one.

@brimoskenya
Copy link

Nice

@StilinskiCyril
Copy link

StilinskiCyril commented Jun 5, 2023

Check this ussd package am creating. It might help yall. https://github.com/StilinskiCyril/ussd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment