国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 SMS verification code interface (Ronglianyun Communications)

SMS verification code interface (Ronglianyun Communications)

Mar 20, 2017 pm 01:41 PM

Steps: 1. Log in to Ronglianyun Communication to register and obtain ACCOUNT SID, AUTH TOKEN, Rest URL (production), AppID (default);

   2. Register a mobile phone number for testing (first register a test number before using it);

  3. Download the demo example and put the code into the project (it is best to create a separate folder for storage).

Code area:

1. Create new test.app.php (controller for testing)

<?php
/*
 * 短信接口測試
 */
class TestApp extends ShoppingbaseApp{
    public function index(){
        $this->display(&#39;test.html&#39;);
    }
    public function get_info()
    {
        include_once(ROOT_PATH.&#39;/includes/rly_message/rest.app.php&#39;);//引入模板文件
        //主帳號,對應(yīng)開官網(wǎng)發(fā)者主賬號下的 ACCOUNT SID
        $accountSid = &#39;&#39;;
        //主帳號令牌,對應(yīng)官網(wǎng)開發(fā)者主賬號下的 AUTH TOKEN
        $accountToken = &#39;&#39;;
        //應(yīng)用Id,在官網(wǎng)應(yīng)用列表中點擊應(yīng)用,對應(yīng)應(yīng)用詳情中的APP ID
        //在開發(fā)調(diào)試的時候,可以使用官網(wǎng)自動為您分配的測試Demo的APP ID
        $appId =&#39;&#39;;
        //請求地址
        //沙盒環(huán)境(用于應(yīng)用開發(fā)調(diào)試):sandboxapp.cloopen.com
        //生產(chǎn)環(huán)境(用戶應(yīng)用上線使用):app.cloopen.com
        $serverIP =&#39;app.cloopen.com&#39;;
        //請求端口,生產(chǎn)環(huán)境和沙盒環(huán)境一致
        $serverPort =&#39;8883&#39;;
        //REST版本號,在官網(wǎng)文檔REST介紹中獲得。
        $softVersion =&#39;2013-12-26&#39;;
        //global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
        $phone_mob = isset($_GET[&#39;phone_mob&#39;]) ? $_GET[&#39;phone_mob&#39;] : &#39;&#39;;
        if($phone_mob){
            $this->sendTemplateSMS($phone_mob,array(&#39;3519&#39;,&#39;5&#39;),"1",$accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion);//手機號碼,替換內(nèi)容數(shù)組,模板ID
        }
    }
    
  /**
  * 發(fā)送模板短信
  * @param to 手機號碼集合,用英文逗號分開
  * @param datas 內(nèi)容數(shù)據(jù) 格式為數(shù)組 例如:array(&#39;Marry&#39;,&#39;Alon&#39;),如不需替換請?zhí)?null
  * @param $tempId 模板Id,測試應(yīng)用和未上線應(yīng)用使用測試模板請?zhí)顚?,正式應(yīng)用上線后填寫已申請審核通過的模板ID
  */       
    public function sendTemplateSMS($to,$datas,$tempId,$accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion)
    {
        // 初始化REST SDK
        $rest = new RestApp($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);

        // 發(fā)送模板短信
        echo "Sending TemplateSMS to $to <br/>";
        $result = $rest->sendTemplateSMS($to,$datas,$tempId);
        if($result == NULL ) {
            echo "result error!";
            exit();
        }
        if($result->statusCode!=0) {
            echo "error code :" . $result->statusCode . "<br>";
            echo "error msg :" . $result->statusMsg . "<br>";
            //TODO 添加錯誤處理邏輯
        }else{
            echo "Sendind TemplateSMS success!<br/>";
            // 獲取返回信息
            $smsmessage = $result->TemplateSMS;
            echo "dateCreated:".$smsmessage->dateCreated."<br/>";
            echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
            //TODO 添加成功處理邏輯
        }
    }
       
}

2. Template file (rest.app.php can be renamed according to the framework you use, or you can use it directly demo’s original name)

<?php
class RestApp {
    private $AccountSid ;   
    private $AccountToken ;  
    private $AppId ;         
    private $ServerIP;       // =&#39;app.cloopen.com&#39;
    private $ServerPort ;    //=&#39;8883&#39;
    private $SoftVersion ;   // =&#39;2013-12-26&#39;
    private $Batch;  //時間戳
    private $BodyType = "xml";//包體格式,可填值:json 、xml
    private $enabeLog = true; //日志開關(guān)??商钪担簍rue、
    private $Filename="./log.txt"; //日志文件
    private $Handle; 
    
    function __construct($ServerIP,$ServerPort,$SoftVersion)    
    {
        $this->RestApp($ServerIP,$ServerPort,$SoftVersion);
    }
    
    function RestApp($ServerIP,$ServerPort,$SoftVersion)
    {
        $this->Batch = date("YmdHis");
        $this->ServerIP = $ServerIP;
        $this->ServerPort = $ServerPort;
        $this->SoftVersion = $SoftVersion;
        $this->Handle = fopen($this->Filename, &#39;a&#39;);
    }
    
   /**
    * 設(shè)置主帳號
    * 
    * @param AccountSid 主帳號
    * @param AccountToken 主帳號Token
    */    
    function setAccount($AccountSid,$AccountToken){
        $this->AccountSid = $AccountSid;
        $this->AccountToken = $AccountToken;   
    }
    
   /**
    * 設(shè)置應(yīng)用ID
    * @param AppId 應(yīng)用ID
    */
    function setAppId($AppId){
        $this->AppId = $AppId; 
    }
    
   /**
    * 打印日志
    * 
    * @param log 日志內(nèi)容
    */
    function showlog($log){
        if($this->enabeLog){
            fwrite($this->Handle,$log."\n");  
        }
    }
    
    /**
     * 發(fā)起HTTPS請求
     */
    function curl_post($url,$data,$header,$post=1)
    {
        //初始化curl
        $ch = curl_init();
        //參數(shù)設(shè)置  
        $res= curl_setopt($ch, CURLOPT_URL,$url);  
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, $post);
        if($post)
        {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        $result = curl_exec($ch);
        //連接失敗
        if($result == FALSE){
            if($this->BodyType==&#39;json&#39;){
                $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"網(wǎng)絡(luò)錯誤\"}";
            } else {
                $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>網(wǎng)絡(luò)錯誤</statusMsg></Response>"; 
            }    
        }
        curl_close($ch);
        return $result;
    } 
    
   /**
    * 發(fā)送模板短信
    * @param to 短信接收彿手機號碼集合,用英文逗號分開
    * @param datas 內(nèi)容數(shù)據(jù)
    * @param $tempId 模板Id
    */       
    function sendTemplateSMS($to,$datas,$tempId)
    {
        //主帳號鑒權(quán)信息驗證,對必選參數(shù)進行判空。
        //$auth=$this->accAuth();
//        if($auth!=""){
//            return $auth;
//        }
        // 拼接請求包體
        if($this->BodyType=="json"){
            $data="";
            for($i=0;$i<count($datas);$i++){
                $data = $data. "&#39;".$datas[$i]."&#39;,"; 
            }
            $body= "{&#39;to&#39;:&#39;$to&#39;,&#39;templateId&#39;:&#39;$tempId&#39;,&#39;appId&#39;:&#39;$this->AppId&#39;,&#39;datas&#39;:[".$data."]}";
        }else{
            $data="";
            for($i=0;$i<count($datas);$i++){
                $data = $data. "<data>".$datas[$i]."</data>"; 
            }
            $body="<TemplateSMS>
                <to>$to</to> 
                <appId>$this->AppId</appId>
                <templateId>$tempId</templateId>
                <datas>".$data."</datas>
                </TemplateSMS>";
        }
        $this->showlog("request body = ".$body);
        // 大寫的sig參數(shù) 
        $sig =  strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
        // 生成請求URL        
        $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
        $this->showlog("request url = ".$url);
        // 生成授權(quán):主帳戶Id + 英文冒號 + 時間戳。
        $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
        // 生成包頭  
        $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
        // 發(fā)送請求
        $result = $this->curl_post($url,$body,$header);
        $this->showlog("response body = ".$result);
        if($this->BodyType=="json"){//JSON格式
            $datas=json_decode($result); 
        }else{ //xml格式
            $datas = simplexml_load_string(trim($result," \t\n\r"));
        }
      //  if($datas == FALSE){
//            $datas = new stdClass();
//            $datas->statusCode = &#39;172003&#39;;
//            $datas->statusMsg = &#39;返回包體錯誤&#39;; 
//        }
        //重新裝填數(shù)據(jù)
        if($datas->statusCode==0){
            if($this->BodyType=="json"){
                $datas->TemplateSMS =$datas->templateSMS;
                unset($datas->templateSMS);   
            }
        }
 
        return $datas; 
    } 
   
  /**
    * 主帳號鑒權(quán)
    */   
   function accAuth()
   {
        if($this->ServerIP==""){
            $data = new stdClass();
            $data->statusCode = &#39;172004&#39;;
            $data->statusMsg = &#39;IP為空&#39;;
          return $data;
        }
        if($this->ServerPort<=0){
            $data = new stdClass();
            $data->statusCode = &#39;172005&#39;;
            $data->statusMsg = &#39;端口錯誤(小于等于0)&#39;;
          return $data;
        }
        if($this->SoftVersion==""){
            $data = new stdClass();
            $data->statusCode = &#39;172013&#39;;
            $data->statusMsg = &#39;版本號為空&#39;;
          return $data;
        } 
        if($this->AccountSid==""){
            $data = new stdClass();
            $data->statusCode = &#39;172006&#39;;
            $data->statusMsg = &#39;主帳號為空&#39;;
          return $data;
        }
        if($this->AccountToken==""){
            $data = new stdClass();
            $data->statusCode = &#39;172007&#39;;
            $data->statusMsg = &#39;主帳號令牌為空&#39;;
          return $data;
        }
        if($this->AppId==""){
            $data = new stdClass();
            $data->statusCode = &#39;172012&#39;;
            $data->statusMsg = &#39;應(yīng)用ID為空&#39;;
          return $data;
        }   
   }
}
?>

Three, front-end template code (test.html)

 <p style="height:100px;border:1px solid gray;text-align:center;padding-top:20px;">
       <form action="" method="post">
             請輸入手機號碼:<input type="text" name="phone_mob" ><input type="button" name="btn" value="點擊獲取驗證碼"><br />
              輸入驗證碼:<input type="text" name="yzm" value=""><br />
              <input type="submit" name="sbt" value="提交">
        </form>
 </p>

Four, js code

<script type="text/javascript">
        $(function(){
            $(&#39;input[name=btn]&#39;).on(&#39;click&#39;,function(){
                var phone_mob = $(&#39;input[name=phone_mob]&#39;).val();
                $.ajax({
                    type: "GET",
                    url: "index.php?app=test&act=get_info",//請求后臺的URL地址
                    dataType: "json",
                    data:{&#39;phone_mob&#39; : phone_mob},
                    success:function(data){
                        if(data.done && data.retval)
                        {   
                            //console.log(data.retval);
                            alert(&#39;成功!&#39;);
                        }
                    }
                });
            });    
        });
    </script>

Related articles:

Implementing SMS verification code interface based on PHP

PHP integrates sending SMS verification code and voice verification code interface functions and usage methods

Tianyi open platform free SMS verification code interface usage examples

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)