Using code to send SMS

Keywords: PHP REST JSON Mobile xml

Interface specification

/**
* initialization
*@ param serverIP required parameter server address
*@ param serverPort required parameter server port
*@ param softVersion required parameter REST version number
*/
REST($serverIP,$serverPort,$softVersion)


/**
*Set primary account
*
*@ param AccountSid master account
*@ param AccountToken primary account Token
*/
function setAccount($AccountSid,$AccountToken)


/**
*Set app ID
*
*@ param AppId application ID
*/
function setAppId($AppId)


/**
*Print log
*
*@ param log content
*/
function showlog($log)


/**
*Initiate HTTPS request
*@ param url request address
*@ param data request package
*@ param header request package header
*@ param post request method defaults to 1 1: post, 0: get
*/
function curl_post($url,$data,$header,$post=1)

/**
*Send template SMS
*@ param to SMS receiving Buddha mobile phone number set, separated by English commas
*The content data format of @ param data is array. For example, array ('Mary ',' alon '). Please fill in null if no replacement is needed.
*@ param $tempId template Id, please fill in 1 for test application and not online application, and fill in the template Id that has been applied for approval after the official application is online.
*/
function sendTemplateSMS($to,$datas,$tempId)

  1 <?php
  2 /*
  3  *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
  4  *
  5  *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
  6  *  that can be found in the LICENSE file in the root of the web site.
  7  *
  8  *   http://www.yuntongxun.com
  9  *
 10  *  An additional intellectual property rights grant can be found
 11  *  in the file PATENTS.  All contributing project authors may
 12  *  be found in the AUTHORS file in the root of the source tree.
 13  */
 14 
 15 
 16 class REST {
 17     private $AccountSid;
 18     private $AccountToken;
 19     private $AppId;
 20     private $ServerIP;
 21     private $ServerPort;
 22     private $SoftVersion;
 23     private $Batch;  //time stamp
 24     private $BodyType = "xml";//Inclusion format, can be filled in: json ,xml
 25     private $enabeLog = true; //Log switch. Fill in value: true,
 26     private $Filename="./log.txt"; //log file
 27     private $Handle; 
 28     function __construct($ServerIP,$ServerPort,$SoftVersion)    
 29     {
 30         $this->Batch = date("YmdHis");
 31         $this->ServerIP = $ServerIP;
 32         $this->ServerPort = $ServerPort;
 33         $this->SoftVersion = $SoftVersion;
 34     $this->Handle = fopen($this->Filename, 'a');
 35     }
 36 
 37    /**
 38     * Set primary account
 39     * 
 40     * @param AccountSid Main account
 41     * @param AccountToken Main account Token
 42     */    
 43     function setAccount($AccountSid,$AccountToken){
 44       $this->AccountSid = $AccountSid;
 45       $this->AccountToken = $AccountToken;   
 46     }
 47     
 48     
 49    /**
 50     * Set app ID
 51     * 
 52     * @param AppId Application of ID
 53     */
 54     function setAppId($AppId){
 55        $this->AppId = $AppId; 
 56     }
 57     
 58    /**
 59     * Print log
 60     * 
 61     * @param log Log content
 62     */
 63     function showlog($log){
 64       if($this->enabeLog){
 65          fwrite($this->Handle,$log."\n");  
 66       }
 67     }
 68     
 69     /**
 70      * Initiate HTTPS request
 71      */
 72      function curl_post($url,$data,$header,$post=1)
 73      {
 74        //Initialization curl
 75        $ch = curl_init();
 76        //Parameter setting  
 77        $res= curl_setopt ($ch, CURLOPT_URL,$url);  
 78        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 79        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 80        curl_setopt ($ch, CURLOPT_HEADER, 0);
 81        curl_setopt($ch, CURLOPT_POST, $post);
 82        if($post)
 83           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 84        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 85        curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
 86        $result = curl_exec ($ch);
 87        //connection failed
 88        if($result == FALSE){
 89           if($this->BodyType=='json'){
 90              $result = "{\"statusCode\":\"172001\",\"statusMsg\":\"network error\"}";
 91           } else {
 92              $result = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Response><statusCode>172001</statusCode><statusMsg>network error</statusMsg></Response>"; 
 93           }    
 94        }
 95 
 96        curl_close($ch);
 97        return $result;
 98      } 
 99 
100    
101     
102    /**
103     * Send template SMS
104     * @param to Collection of mobile phone numbers of SMS receivers, separated by English commas
105     * @param datas Content data
106     * @param $tempId Template Id
107     */       
108     function sendTemplateSMS($to,$datas,$tempId)
109     {
110         //Verify the authentication information of the primary account, and judge the required parameters as null.
111         $auth=$this->accAuth();
112         if($auth!=""){
113             return $auth;
114         }
115         // Splicing request package
116         if($this->BodyType=="json"){
117            $data="";
118            for($i=0;$i<count($datas);$i++){
119               $data = $data. "'".$datas[$i]."',"; 
120            }
121            $body= "{'to':'$to','templateId':'$tempId','appId':'$this->AppId','datas':[".$data."]}";
122         }else{
123            $data="";
124            for($i=0;$i<count($datas);$i++){
125               $data = $data. "<data>".$datas[$i]."</data>"; 
126            }
127            $body="<TemplateSMS>
128                     <to>$to</to> 
129                     <appId>$this->AppId</appId>
130                     <templateId>$tempId</templateId>
131                     <datas>".$data."</datas>
132                   </TemplateSMS>";
133         }
134         $this->showlog("request body = ".$body);
135         // Capitalized sig parameter 
136         $sig =  strtoupper(md5($this->AccountSid . $this->AccountToken . $this->Batch));
137         // Generate request URL        
138         $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
139         $this->showlog("request url = ".$url);
140         // Build authorization: master account Id + English colon + Timestamp.
141         $authen = base64_encode($this->AccountSid . ":" . $this->Batch);
142         // Generating Baotou  
143         $header = array("Accept:application/$this->BodyType","Content-Type:application/$this->BodyType;charset=utf-8","Authorization:$authen");
144         // Send request
145         $result = $this->curl_post($url,$body,$header);
146         $this->showlog("response body = ".$result);
147         if($this->BodyType=="json"){//JSON format
148            $datas=json_decode($result); 
149         }else{ //xml format
150            $datas = simplexml_load_string(trim($result," \t\n\r"));
151         }
152       //  if($datas == FALSE){
153 //            $datas = new stdClass();
154 //            $datas->statusCode = '172003';
155 //            $data - > statusmsg = 'return package error'; 
156 //        }
157         //Reload data
158         if($datas->statusCode==0){
159          if($this->BodyType=="json"){
160             $datas->TemplateSMS =$datas->templateSMS;
161             unset($datas->templateSMS);   
162           }
163         }
164  
165         return $datas; 
166     } 
167    
168   /**
169     * Main account authentication
170     */   
171    function accAuth()
172    {
173        if($this->ServerIP==""){
174             $data = new stdClass();
175             $data->statusCode = '172004';
176             $data->statusMsg = 'IP Empty';
177           return $data;
178         }
179         if($this->ServerPort<=0){
180             $data = new stdClass();
181             $data->statusCode = '172005';
182             $data->statusMsg = 'Port error (less than or equal to 0)';
183           return $data;
184         }
185         if($this->SoftVersion==""){
186             $data = new stdClass();
187             $data->statusCode = '172013';
188             $data->statusMsg = 'Version number is empty';
189           return $data;
190         } 
191         if($this->AccountSid==""){
192             $data = new stdClass();
193             $data->statusCode = '172006';
194             $data->statusMsg = 'Primary account number is empty';
195           return $data;
196         }
197         if($this->AccountToken==""){
198             $data = new stdClass();
199             $data->statusCode = '172007';
200             $data->statusMsg = 'Primary account token is empty';
201           return $data;
202         }
203         if($this->AppId==""){
204             $data = new stdClass();
205             $data->statusCode = '172012';
206             $data->statusMsg = 'application ID Empty';
207           return $data;
208         }   
209    }
210 }
211 ?>
 1 <?php
 2 /*
 3  *  Copyright (c) 2014 The CCP project authors. All Rights Reserved.
 4  *
 5  *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
 6  *  that can be found in the LICENSE file in the root of the web site.
 7  *
 8  *   http://www.yuntongxun.com
 9  *
10  *  An additional intellectual property rights grant can be found
11  *  in the file PATENTS.  All contributing project authors may
12  *  be found in the AUTHORS file in the root of the source tree.
13  */
14 
15 include_once("./CCPRestSmsSDK.php");
16 
17 //Main account,Corresponding to the main account number of the Issuer on the official website ACCOUNT SID
18 $accountSid= '';
19 
20 //Primary account token,Corresponding to the AUTH TOKEN
21 $accountToken= '';
22 
23 //application Id,Click the application in the official website application list, corresponding to the APP ID
24 //During development and debugging, you can use the APP ID of the test Demo automatically assigned to you on the official website.
25 $appId='';
26 
27 //Request address
28 //Sandbox environment (for application development and debugging): sandboxapp.cloopen.com
29 //Production environment (online use of user application): app.cloopen.com
30 $serverIP='app.cloopen.com';
31 
32 
33 //Request port, production environment and sandbox environment are consistent
34 $serverPort='8883';
35 
36 //REST Version number, on the official website REST Obtained in the introduction.
37 $softVersion='2013-12-26';
38 
39 
40 /**
41   * Send template SMS
42   * @param to Collection of mobile phone numbers, separated by commas
43   * @param datas The format of content data is array. For example, array ('Mary ',' alon '). Please fill in null if no replacement is needed.
44   * @param $tempId Please fill in 1 for template ID, test application and non online application, and fill in the template ID that has been applied for approval after the official application is online.
45   */       
46 function sendTemplateSMS($to,$datas,$tempId)
47 {
48      // Initialization REST SDK
49      global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
50      $rest = new REST($serverIP,$serverPort,$softVersion);
51      $rest->setAccount($accountSid,$accountToken);
52      $rest->setAppId($appId);
53     
54      // Send template SMS
55      echo "Sending TemplateSMS to $to <br/>";
56      $result = $rest->sendTemplateSMS($to,$datas,$tempId);
57      if($result == NULL ) {
58          echo "result error!";
59          break;
60      }
61      if($result->statusCode!=0) {
62          echo "error code :" . $result->statusCode . "<br>";
63          echo "error msg :" . $result->statusMsg . "<br>";
64          //TODO Add error handling logic
65      }else{
66          echo "Sendind TemplateSMS success!<br/>";
67          // Get return information
68          $smsmessage = $result->TemplateSMS;
69          echo "dateCreated:".$smsmessage->dateCreated."<br/>";
70          echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
71          //TODO Add successful processing logic
72      }
73 }
74 
75 //Demo call 
76         //**************************************Examples***********************************************************************
77         //*If you use the APP ID of the test Demo, you need to use the default template ID 1. The sending mobile number is 1380000000, and the incoming parameters are 6532 and 5. Then the calling method is*
78         //*result = sendTemplateSMS("13800000000" ,array('6532','5'),"1");                                                                          *
79         //*Then the message received by the mobile number 1380000000 is: [cloud communication] you are using the cloud communication SMS template, your verification code is 6532, please input it correctly within 5 minutes.*
80         //*********************************************************************************************************************
81 sendTemplateSMS("",array('',''),"");//Cell phone number, alternative content array, template ID
82 ?>

Posted by fredyap1234 on Thu, 17 Oct 2019 12:54:51 -0700