Создание USSD Mutilevel меню в php - PullRequest
0 голосов
/ 24 октября 2018

Я разрабатываю USSD-меню на php, которое предназначено для многоуровневого меню. Структура выглядит следующим образом:

Когда пользователь набирает *2000* 22 #, отображается, например,

  1. Предметы домашнего обихода
  2. Электронные устройства
  3. Выход.

Когда пользователь отвечает «1», отображается подменю предметов домашнего обихода, как показано ниже: 1. Диван2. Газонокосилка

Мой вопрос заключается в том, как отобразить второе подменю для «1. Couch», если пользователь снова выбирает «1».Ниже мой код:

 $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);


?>

1 Ответ

0 голосов
/ 16 ноября 2018

Вы можете использовать отношения для извлечения связанных элементов на основе меню, выбранного пользователем.

...