我正在用php开发USSD菜单,该菜单旨在用作多级菜单 结构是这样的:
用户拨打* 2000 * 22#时显示例如:
当用户回复“ 1”时,将显示“家庭用品”子菜单,如下所示: 1.沙发 2.割草机
我的问题是,如果用户再次选择“ 1”,如何显示“ 1. Couch”的第二个子菜单。下面是我的代码:
$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: \n";
$response .= "1. Household Items \n";
$response .= "2. Electronic Devices \n";
$response .= "3. Exit \n";
$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 \n";
$response2 .= "2. Lawn mower\n";
$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);
?>
答案 0 :(得分:0)
您可以根据用户选择的菜单使用关系来获取相关项目。
答案 1 :(得分:0)
对于USSD,我们有等级。我们检查拨号器能够知道的级别在哪个菜单上。这是你应该怎么做
初始化$parts
和$level
$text = $_POST['text'];//this is text the user has dialed
$level = 0;
$parts = [];
if(strlen($text) == 0 ) {
$level = 0;
} else {
$parts = explode("*", $text);
$level = count($parts);
}
用户首次拨打代码
if ($level == 0){
//Display main menu
$response = "CON Reply with: \n";
$response .= "1. Household Items \n";
$response .= "2. Electronic Devices \n";
$response .= "3. Exit \n";
}
用户第二次拨打菜单
elseif($level == 1){
//get the number he dialed
if ($parts[0] == 1){
//Display menu
$response = "CON 1. Couch\n";
$response .= "2. Lawn mower";
}elseif ($parts[0] == 2){
//Display other menus and so on
}else{
//bla bla
}
用户第三次拨打菜单
elseif($level == 2){
//get the number he dialed
if ($parts[0] == 1){
//Display menu
$response = "CON 1. Couch";
}elseif ($parts[0] == 2){
//Display other menus and so on
}else{
//bla bla
}
希望有帮助。如有任何疑问,请随时提问