首先,註冊一個容聯雲通訊的賬號,然後創建一個應用。
然後在號碼管理那裏綁定測試號碼。可以查看官方文檔:https://doc.yuntongxun.com/p/5a531a353b8496dd00dcdfe2
再然後,開幹,話不多說,直接上代碼:
前端(忒簡陋哈,只是測試能否獲取):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js" type="text/javascript" charset="utf-8"></script> </head> <body> <button id="dd">點擊調用</button> </body> <script type="text/javascript"> $("#dd").click(function(){ $.ajax({ url: "http://127.0.0.1/sms/Demo/SendTemplateSMS.php", data:{ phone:1331144797 }, success: function(res){ console.log(res) }}); }) </script> </html>
php代碼(SendTemplateSMS.php):看不懂參數的看官方文檔:https://doc.yuntongxun.com/p/5a533e0c3b8496dd00dce08c
<?php /* * Copyright (c) 2014 The CCP project authors. All Rights Reserved. * * Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license * that can be found in the LICENSE file in the root of the web site. * * http://www.yuntongxun.com * * An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ include_once("../SDK/CCPRestSDK.php"); //主帳號 $accountSid= '8aaf070863b5263bf1a011806ee'; //主帳號Token $accountToken= 'c30beebd39db4835d1d68d36db6'; //應用Id $appId='8aaf0708780055cd01ec83fa5'; //請求地址,格式如下,不需要寫https:// $serverIP='app.cloopen.com'; //請求端口 $serverPort='8883'; //REST版本號 $softVersion='2013-12-26'; /** * 發送模板短信 * @param to 手機號碼集合,用英文逗號分開 * @param datas 內容數據 格式爲數組 例如:array('Marry','Alon'),如不需替換請填 null * @param $tempId 模板Id */ function sendTemplateSMS($to,$datas,$tempId) { // 初始化REST SDK global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion; $rest = new REST($serverIP,$serverPort,$softVersion); $rest->setAccount($accountSid,$accountToken); $rest->setAppId($appId); // 發送模板短信 echo "Sending TemplateSMS to $to "; $result = $rest->sendTemplateSMS($to,$datas,$tempId); if($result == NULL ) { echo "result error!"; } if($result->statusCode!=0) { echo "模板短信發送失敗! "; echo "error code :" . $result->statusCode . " "; echo "error msg :" . $result->statusMsg . " "; //下面可以自己添加錯誤處理邏輯 }else{ echo "模板短信發送成功! "; // 獲取返回信息 $smsmessage = $result->TemplateSMS; echo "dateCreated:".$smsmessage->dateCreated." "; echo "smsMessageSid:".$smsmessage->smsMessageSid." "; //下面可以自己添加成功處理邏輯 } } //Demo調用,參數填入正確後,放開註釋可以調用 sendTemplateSMS("13897,16601161",array(str_pad(mt_rand(10, 999999), 6, "0", STR_PAD_BOTH),2),1); //sendTemplateSMS("手機號碼集合",array(生成的隨機驗證碼,2),1); ?>
目錄結構:
實測真實可用,上面的電話號碼以及Appid之類的要用的朋友需要改哈