1.國外短信這里介紹:短信寶
用的是thinkphp框架
在TP模塊下的目錄新建Service文件夾,在里面新建service服務類用于短信寶發送短信。
更多詳情可參考官網:
namespaceHome\Service;
classSmaoService{
function__construct(){
$config=array(
'user'=D('Config')-get('msg_user'),
'pass'=D('Config')-get('msg_pass'),
'smsapi'='接口地址',
);
$statusCode=array(
0=短信發送成功,
-1=參數不全,
-2=服務器空間不支持,請確認支持curl或者fsocket,聯系您的空間商解決或者更換空間!,
30=密碼錯誤,
40=賬號不存在,
41=余額不足,
42=帳戶已過期,
43=IP地址限制,
50=內容含有敏感詞
);
$this-config=$config;
$this-statusCode=$statusCode;
}
/**
*發送驗證碼
*@param$phone
*@param$code驗證碼
*@returnmixed
*/
publicfunctionsendMsg($phone,$code){
$config=$this-config;
$statusCode=$this-statusCode;
$autograph=D('Config')-get('msg_autograph');
$content=D('Config')-get('msg_content');
$content='【'.$autograph.'】'.str_replace('{code}',$code,$content);
$sendurl=$config['smsapi'].sms?u=.$config['user'].p=.md5($config['pass']).m=.$phone.c=.urlencode($content);
$status=file_get_contents($sendurl);
$reData['status']=$status;
$reData['info']=$statusCode[$status];
return$reData;
}
}
數據庫中要建配置參數:
msg_user(短信寶用戶名)
msg_pass(短信寶密碼)
msg_autograph(短信寶簽名)
msg_content(短信內容,必須包含驗證碼替換符{code})
2.國外的介紹QcloudS騰訊短信
可以發送國內短信的接口有很多,之前也有整理比較好用的。當有用戶需要發送國際短信時,比較常用就是阿里和騰訊的了。下面是騰訊短信簡單發送對接TP的Service
下面以國際短信為例
1、在騰訊短信控制臺申請海外文本短信,只需要申請短信模板即可,海外短信不需要申請簽名,國內短信必須申請簽名。
2、下載官方的phpSDK,在TP使用時需要小改一下命名空間,文章下有提供可下載。放到TP的Vendor/Qcloudsms文件夾中。
3、在要使用的模塊下新建QcloudsmsService.class.php,代碼如下:
<!--?phpnamespaceHome\Service;
classQcloudsmsService{
function__construct(){
$config=array(
'appid'='14054******',//控制臺查看
'appkey'='6fe55********************',//控制臺查看
'templId'='295555**',
'nationCode'='852555',//國家或地區區號,香港852,大陸86
);
$this-config=$config;
}
/**
*發送驗證碼
*@param$phone
*@param$code驗證碼
*@returnmixed
*/
publicfunctionsendMsg($phone,$code){
vendor('Qcloudsms.SmsSender');
$config=$this-config;
$singleSender=new\SmsSingleSender($config['appid'],$config['appkey']);
//普通單發
$result=$singleSender-send(0,$config['nationCode'],$phone,您好,您的驗證碼為.$code,,);
//返回的成功示例:{result:0,errmsg:OK,ext:,sid:2:670479-0268698729-028972-001510040916,fee:1}
//result為0表示發送成功
$rsp=json_decode($result,true);
return$rsp;
}
}