用【云片網(wǎng)】為案例
原生php
<?php
header("Content-Type:text/html;charset=utf-8");
$apikey = ""; //修改為您的apikey(https://www.yunpian.com)登錄官網(wǎng)后獲取
$code=rand(1000,9999);//生成一個隨機數(shù)
$mobile = ""; //填寫接受驗證碼的手機號代替
$text="【xxx】您的驗證碼是$code";//xxx為云片網(wǎng)的簽名
$ch = curl_init();
/* 設置驗證方式 */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
? ? 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
/* 設置返回結果為流 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* 設置超時時間*/
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
/* 設置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 取得用戶信息
$json_data = get_user($ch,$apikey);
$array = json_decode($json_data,true);
echo '<pre>';print_r($array);
// 發(fā)送短信
$data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
$json_data = send($ch,$data);
$array = json_decode($json_data,true);
echo '<pre>';print_r($array);
// // 發(fā)送模板短信
// // 需要對value進行編碼
// $data = array('tpl_id' => '1', 'tpl_value' => ('#code#').
//? ? '='.urlencode('1234').
//? ? '&'.urlencode('#company#').
//? ? '='.urlencode('歡樂行'), 'apikey' => $apikey, 'mobile' => $mobile);
// print_r ($data);
// $json_data = tpl_send($ch,$data);
// $array = json_decode($json_data,true);
// echo '<pre>';print_r($array);
// // 發(fā)送語音驗證碼
// $data=array('code'=>'9876','apikey'=>$apikey,'mobile'=>$mobile);
// $json_data =voice_send($ch,$data);
// $array = json_decode($json_data,true);
// echo '<pre>';print_r($array);
// // 發(fā)送語音通知,務必要報備好模板
// /*
// 模板: 課程#name#在#time#開始。 最終發(fā)送結果: 課程深度學習在14:00開始
//? */
//? $tpl_id = '123456';//你自己后臺報備的模板id
//? $tpl_value = urlencode('name=深度學習&time=14:00');
//? $data = array('tpl_id'=>$tpl_id,'tpl_value'=>$tpl_value,'apikey'=>$apikey,'mobile'=>$mobile);
//? $json_data = notify_send($ch,$data);
//? $array = json_decode($json_data,true);
//? print_r($array);
curl_close($ch);
/************************************************************************************/
//獲得賬戶
function get_user($ch,$apikey){
? ? curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function send($ch,$data){
? ? curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function tpl_send($ch,$data){
? ? curl_setopt ($ch, CURLOPT_URL,
? ? ? ? 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function voice_send($ch,$data){
? ? curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function notify_send($ch,$data){
? ? curl_setopt ($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function checkErr($result,$error) {
? ? if($result === false)
? ? {
? ? ? ? echo 'Curl error: ' . $error;
? ? }
? ? else
? ? {
? ? ? ? //echo '操作完成沒有任何錯誤';
? ? }
}
?>
使用thinkphp5框架
前臺頁面

<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <meta http-equiv="X-UA-Compatible" content="ie=edge">
? ? <title>Document</title>
? ? <script type="text/javascript" src="__STATIC__/js/jquery-3.3.1.min.js"></script>
</head>
<body>
? ? <form method ="post" action="{:url('reg')}">
? ? ? ? 手機:<input type="text" name="mobile" id="mobile" required>
? ? ? ? <input type="button" value="發(fā)送驗證碼" id="sendbtn">
? ? ? ? <input type="text" name="code" id="code">
? ? ? ? <input type="submit" value="注冊">
? ? </form>
? ? <script type="text/javascript">
? ? ? ? $(document).ready(function(){
? ? ? ? ? $("#sendbtn").click(function(){
? ? ? ? ? ? var mobile=$("#mobile").val();
? ? ? ? $.get("{:url('sendmessage')}", { mobile:mobile },
? function(data){
? ? alert("Data Loaded: " + data);
? });
? });
});
? ? ? ? </script>
</body>
</html>
控制臺
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Controller
{
? ? public function index()
? ? {
? ? ? ? return $this->fetch();
? ? }
? ? public function reg()
? ? {
? ? ? $code=input('code');
? ? ? if($code!=$_COOKIE["code"])
? ? ? ? $this->error('手機驗證碼錯誤');
? ? ? ? else{
? ? ? ? ? ? $this->success('注冊成功');
? ? ? ? }
? ? }
//短信
? ? public function sendmessage($mobile)
? ? {
? ? ? ? header("Content-Type:text/html;charset=utf-8");
? ? ? ? // $apikey = "xxx"; //修改為您的apikey(https://www.yunpian.com)登錄官網(wǎng)后獲取
? ? ? ? $code=rand(1000,9999);
? ? ? ? // $mobile = "15278310544"; //請用自己的手機號代替
? ? ? ? setcookie("code",$code);
? ? ? ? $text="【云片網(wǎng)】您的驗證碼是$code";
? ? ? ? $ch = curl_init();
? ? ? ? /* 設置驗證方式 */
? ? ? ? curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
? ? ? ? ? ? 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
? ? ? ? /* 設置返回結果為流 */
? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
? ? ? ? /* 設置超時時間*/
? ? ? ? curl_setopt($ch, CURLOPT_TIMEOUT, 10);
? ? ? ? /* 設置通信方式 */
? ? ? ? curl_setopt($ch, CURLOPT_POST, 1);
? ? ? ? curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
? ? ? ? // 取得用戶信息
? ? ? ? $json_data = get_user($ch,$apikey);
? ? ? ? $array = json_decode($json_data,true);
? ? ? ? // echo '<pre>';print_r($array);
? ? ? ? // 發(fā)送短信
? ? ? ? $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
? ? ? ? $json_data = send($ch,$data);
? ? ? ? $array = json_decode($json_data,true);
? ? ? ? echo $array['msg'];
? ? ? ?? curl_close($ch);
? ? }
?}
在common.php添加公共代碼
// 應用公共文件
//獲得賬戶
function get_user($ch,$apikey){
? ? curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function send($ch,$data){
? ? curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function tpl_send($ch,$data){
? ? curl_setopt ($ch, CURLOPT_URL,
? ? ? ? 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function voice_send($ch,$data){
? ? curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function notify_send($ch,$data){
? ? curl_setopt ($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');
? ? curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
? ? $result = curl_exec($ch);
? ? $error = curl_error($ch);
? ? checkErr($result,$error);
? ? return $result;
}
function checkErr($result,$error) {
? ? if($result === false)
? ? {
? ? ? ? echo 'Curl error: ' . $error;
? ? }
? ? else
? ? {
? ? ? ? //echo '操作完成沒有任何錯誤';
? ? }
}
注:前端頁面應用jQuery傳參,通過 setcookie("code",$code)保存隨機碼