Advertisement
Guest User

Untitled

a guest
Jul 13th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. <?php
  2. /* Simple sample USSD registration application
  3. * USSD gateway that is being used is Africa's Talking USSD gateway
  4. */
  5.  
  6. // Print the response as plain text so that the gateway can read it
  7. header('Content-type: text/plain');
  8.  
  9. /* local db configuration */
  10. $dsn = 'mysql:dbname=dbname;host=127.0.0.1;'; //database name
  11. $user = 'your user'; // your mysql user
  12. $password = 'your password'; // your mysql password
  13.  
  14. // Create a PDO instance that will allow you to access your database
  15. try {
  16. $dbh = new PDO($dsn, $user, $password);
  17. }
  18. catch(PDOException $e) {
  19. //var_dump($e);
  20. echo("PDO error occurred");
  21. }
  22. catch(Exception $e) {
  23. //var_dump($e);
  24. echo("Error occurred");
  25. }
  26.  
  27. // Get the parameters provided by Africa's Talking USSD gateway
  28. $phone = $_GET['phoneNumber'];
  29. $session_id = $_GET['sessionId'];
  30. $service_code = $_GET['serviceCode'];
  31. $ussd_string= $_GET['text'];
  32.  
  33. //set default level to zero
  34. $level = 0;
  35.  
  36. /* Split text input based on asteriks(*)
  37. * Africa's talking appends asteriks for after every menu level or input
  38. * One needs to split the response from Africa's Talking in order to determine
  39. * the menu level and input for each level
  40. * */
  41. $ussd_string_exploded = explode ("*",$ussd_string);
  42.  
  43. // Get menu level from ussd_string reply
  44. $level = count($ussd_string_exploded);
  45.  
  46. if($level == 1 or $level == 0){
  47.  
  48. display_menu(); // show the home/first menu
  49. }
  50.  
  51. if ($level > 1)
  52. {
  53.  
  54. if ($ussd_string_exploded[0] == "1")
  55. {
  56. // If user selected 1 send them to the registration menu
  57. register($ussd_string_exploded,$phone, $dbh);
  58. }
  59.  
  60. else if ($ussd_string_exploded[0] == "2"){
  61. //If user selected 2, send them to the about menu
  62. about($ussd_string_exploded);
  63. }
  64. }
  65.  
  66. /* The ussd_proceed function appends CON to the USSD response your application gives.
  67. * This informs Africa's Talking USSD gateway and consecuently Safaricom's
  68. * USSD gateway that the USSD session is till in session or should still continue
  69. * Use this when you want the application USSD session to continue
  70. */
  71. function ussd_proceed($ussd_text){
  72. echo "CON $ussd_text";
  73. }
  74.  
  75. /* This ussd_stop function appends END to the USSD response your application gives.
  76. * This informs Africa's Talking USSD gateway and consecuently Safaricom's
  77. * USSD gateway that the USSD session should end.
  78. * Use this when you to want the application session to terminate/end the application
  79. */
  80. function ussd_stop($ussd_text){
  81. echo "END $ussd_text";
  82. }
  83.  
  84. //This is the home menu function
  85. function display_menu()
  86. {
  87. $ussd_text = "1. Register \n 2. About \n"; // add \n so that the menu has new lines
  88. ussd_proceed($ussd_text);
  89. }
  90.  
  91.  
  92. // Function that hanldles About menu
  93. function about($ussd_text)
  94. {
  95. $ussd_text = "This is a sample registration application";
  96. ussd_stop($ussd_text);
  97. }
  98.  
  99. // Function that handles Registration menu
  100. function register($details,$phone, $dbh){
  101. if(count($details) == 2)
  102. {
  103.  
  104. $ussd_text = "Please enter your Full Name and Email, each seperated by commas:";
  105. ussd_proceed($ussd_text); // ask user to enter registration details
  106. }
  107. if(count($details)== 3)
  108. {
  109. if (empty($details[1])){
  110. $ussd_text = "Sorry we do not accept blank values";
  111. ussd_proceed($ussd_text);
  112. } else {
  113. $input = explode(",",$details[1]);//store input values in an array
  114. $full_name = $input[0];//store full name
  115. $email = $input[1];//store email
  116. $phone_number =$phone;//store phone number
  117.  
  118. // build sql statement
  119. $sth = $dbh->prepare("INSERT INTO customer (full_name, email, phone) VALUES('$full_name','$email','$phone_number')");
  120. //execute insert query
  121. $sth->execute();
  122. if($sth->errorCode() == 0) {
  123. $ussd_text = $full_name." your registration was successful. Your email is ".$email." and phone number is ".$phone_number;
  124. ussd_proceed($ussd_text);
  125. } else {
  126. $errors = $sth->errorInfo();
  127. }
  128. }
  129. }
  130. }
  131. # close the pdo connection
  132. $dbh = null;
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement