Skip to content

Instantly share code, notes, and snippets.

@abel-masila
Created July 13, 2016 09:16
Show Gist options
  • 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;
?>
@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