1. 程式人生 > >聚合API-短信接口,THINKPHP整合聚合api短信接口

聚合API-短信接口,THINKPHP整合聚合api短信接口

內容 .cn value mes 現在 doc form for 其他

覺得發個短信驗證碼挺簡單的,但是之前沒寫過,就想寫一個看看,阿裏大於的太貴了,只能用其他平臺的數據接口,

第一步:聚合官網登錄、註冊,網址如下:https://www.juhe.cn/login(登錄),https://www.juhe.cn/register(註冊)

第二步:申請數據-短信api服務,網址如下:https://www.juhe.cn/docs/api/id/54,申請數據, 申請的時候送10條數據,如果想以後也想用,可以選擇現在特價的9元300條數據。

第三步:進入個人中心,我的數據,選擇短信API服務,進行認證,待認證完成即可。

第四步:進入個人中心,我的數據,選擇短信API服務,選擇模板,個人選擇“

  • 添加自定義模板 (*由於您是未實名或個人用戶,暫只能使用系統固定的模板和無變量的模板)”,設置短信模板。待審核成功即可。

第五步:寫代碼

聚合提供實例代碼位置:https://code.juhe.cn/docs/168

請記住以下幾個數據:AppKey,模板ID

【前端代碼】

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<form action="{:U(‘**‘/**)}" method="post">
		<input type="text" name="userphone" required="required">
		<input type="submit" value="提交">
	</form>
</body>
</html>

【PHP代碼】

<?php
namespace Home\Controller;
use Think\Controller;
class DxController extends Controller{
    //短信接口
    public function send(){
        if(IS_POST){
            $phone = I(‘post.userphone‘);
        //此處應該有驗證
header(‘content-type:text/html;charset=utf-8‘); $sendUrl
= ‘http://v.juhe.cn/sms/send‘; //短信接口的URL $code = rand(0001,9999);//驗證碼生成格式,請生成4-8位,數字或字母隨機組合 $smsConf = array( "key" => "************", //您申請的APPKEY "mobile" => "$phone", //接受短信的用戶手機號碼 "tpl_id" => "*****", //您申請的短信模板ID,根據實際情況修改 "tpl_value" => "#code#=".$code."&#company#=聚合數據"//您設置的模板變量,根據實際情況修改 ); $content = $this->juhecurl($sendUrl,$smsConf,1); //請求發送短信 if($content){ $result = json_decode($content,true); $error_code = $result[‘error_code‘]; if($error_code == 0){ //狀態為0,說明短信發送成功 echo "短信發送成功,短信ID:".$result[‘result‘][‘sid‘]; }else{ //狀態非0,說明失敗 $msg = $result[‘reason‘]; echo "短信發送失敗(".$error_code."):".$msg; } }else{ //返回內容異常,以下可根據業務邏輯自行修改 echo "請求發送短信失敗"; } }else{ $this->display(); } } //curl請求 public function juhecurl($url,$params=false,$ispost=0){ $httpInfo = array(); $ch = curl_init(); curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 ); curl_setopt( $ch, CURLOPT_USERAGENT , ‘Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22‘ ); curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 ); curl_setopt( $ch, CURLOPT_TIMEOUT , 30); curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true ); if( $ispost ){ curl_setopt( $ch , CURLOPT_POST , true ); curl_setopt( $ch , CURLOPT_POSTFIELDS , $params ); curl_setopt( $ch , CURLOPT_URL , $url ); }else{ if($params){ curl_setopt( $ch , CURLOPT_URL , $url.‘?‘.$params ); }else{ curl_setopt( $ch , CURLOPT_URL , $url); } } $response = curl_exec( $ch ); if ($response === FALSE) { //echo "cURL Error: " . curl_error($ch); return false; } $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE ); $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) ); curl_close( $ch ); return $response; } }

第六步:短信驗證碼驗證等操作請自行填寫,謝謝!

------------------------轉載請註明出處-----------------------

-----------------------QQ:297760026-----------------------

-----------------------www.sl95.cn -----------------------

聚合API-短信接口,THINKPHP整合聚合api短信接口