2

我正在尝试在供应商的帮助下进行 OTP 验证,以便向应用程序用户发送消息。供应商向我提供了 request_sms.php 文件,我必须在其中集成 api 密钥。我在本地尝试过,它运行良好,没有任何问题。我在我的服务器上尝试了同样的方法,我没有收到任何关于 OTP 的短信,但这些值存储在我的数据库中。任何帮助将不胜感激。谢谢

<?php

include './include/DbHandler.php';
$db = new DbHandler();


$response = array();

// echo $_POST['mobile'];

if (isset($_POST['mobile']) && $_POST['mobile'] != '') {

$name = $_POST['name'];
 $email = $_POST['email'];
 $mobile = $_POST['mobile'];

 $otp = rand(100000, 999999);

 $res = $db->createUser($name, $email, $mobile, $otp);

  if ($res == USER_CREATED_SUCCESSFULLY) {

     // send sms
   sendSms($mobile, $otp);

   $response["error"] = false;
   $response["message"] = "SMS request is initiated! You will be receiving it shortly.";
 } else if ($res == USER_CREATE_FAILED) {
   $response["error"] = true;
   $response["message"] = "Sorry! Error occurred in registration.";
 } else if ($res == USER_ALREADY_EXISTED) {
   $response["error"] = true;
   $response["message"] = "Mobile number already existed!";
 }
} else {
  $response["error"] = true;
    $response["message"] = "Sorry! mobile number is not valid or missing.";
}

echo json_encode($response);

 function sendSms($mobile, $otp) {

  $otp_prefix = ':';

 //Your message to send, Add URL encoding here.
   $message = urlencode("Hello! Your OTP is '$otp_prefix $otp'");

   $response_type = 'json';

  //Define route 
    $route = "4";

   //Prepare you post parameters
      $postData = array(
      'authkey' => AUTH_KEY,
       'mobiles' => $mobile,
       'message' => $message,
     'sender' => SENDER_ID,
     'route' => $route,
     'response' => $response_type
    );

    //API URL
    //    $url = "https://control.otp.com/sendhttp.php";
     $url = "https://control.otp.com/api/sendhttp.php?  authkey=89976A2FHkWCE4eCC58c4637e&mobiles=9677******&message=OTP%20verify&sender=POWTER&route=4";

   // init the resource
   $ch = curl_init();
    curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData
       //,CURLOPT_FOLLOWLOCATION => true
    ));


    //Ignore SSL certificate verification
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);


   //get response
   $output = curl_exec($ch);

  //Print error if any
    if (curl_errno($ch)) {
     echo 'error:' . curl_error($ch);
    }  

     curl_close($ch);
     }


   ?>
4

0 回答 0