在php中创建USSD多级菜单
问题描述:
Am developing a USSD menu in php which is intended to be a multilevel menu The structure is like this:
When a user dials *2000*22# it displays eg:
- Household items
- Electronic Devices
- Exit.
When a user responds with "1" a submenu of Household items displays as seen below: 1. Couch 2. Lawn mower
My question is how do I display a second submenu for "1. Couch" if the user selects "1" again. Below is my code:
$ussdRequest = json_decode(file_get_contents('php://input'), true);
$obj = json_decode($json);
$timestamp = $ussdRequest["timestamp"];
$sessionId = $ussdRequest["sessionId"];
$serviceCode = "*2000*22#";
$phoneNumber = $ussdRequest["MSISDN"];
$cpId = $ussdRequest["cpId"];
$cpPassword = $ussdRequest["cpPassword"];
$opType = $ussdRequest["opType"];
$msgCoding = $ussdRequest["msgCoding"];
$ussdContent=$ussdRequest["ussdContent"];
//Display main menu
$response = "Reply with:
";
$response .= "1. Household Items
";
$response .= "2. Electronic Devices
";
$response .= "3. Exit
";
$date = new DateTime($date, new DateTimeZone('UTC'));
$newtimestamp = $date->format('YmdHmsn');
$cpPassword2 = "7809834";
$hashparams = $cpId.$cpPassword2.$newtimestamp;
$hashpasswd = md5($hashparams);
$data = [ "sessionId" => $sessionId, "cpId" => $cpId,"cpPassword" =>$hashpasswd,"serviceCode"=>$serviceCode,"msgType"=>1,"opType"=>$opType,"msgCoding"=>$msgCoding,"ussdContent"=>$response,"timeStamp"=>$newtimestamp,"MSISDN"=>$phoneNumber];
$ussdc = $ussdContent;
switch($ussdc){
// if the user responds with "1" the ussd content shows menu below
case "1":
$response2 .= "1. Couch
";
$response2 .= "2. Lawn mower
";
$data2 = [ "sessionId" => $sessionId, "cpId" => $cpId,"cpPassword" =>$hashpasswd,"serviceCode"=>$serviceCode,"msgType"=>1,"opType"=>1,"msgCoding"=>$msgCoding,"ussdContent"=>$response2,"timeStamp"=>$newtimestamp,"MSISDN"=>$phoneNumber];
//API URL
$url = '58.278.901.54:8581/push_ussd';
//create a new cURL resource
$ch = curl_init($url);
$decoded_data = json_encode($data2);
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $decoded_data);
//set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the POST request
$result = curl_exec($ch);
break;
//API URL
$url = '58.278.901.54:8581/push_ussd';
//create a new cURL resource
$ch = curl_init($url);
$decoded_data = json_encode($data);
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $decoded_data);
//set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the POST request
$result = curl_exec($ch);
if ($result === FALSE) {
echo curl_error($ch);
//ini_set("error_log", "/tmp/php-error.log");
exit();
}
//close cURL resource
curl_close($ch);
$response = [ "sessionId" => $sessionId, "msisdn"=>$phoneNumber,"errorCode"=>"200","errorMsg"=>"Success"];
header('Content-type: application/json');
echo json_encode($response);
?>
答
You can use relationships to fetch the related items based on the menu selected by a user.