[PHP高可用后端]④0--表设计、阿里短信SDK
2017-11-29 本文已影响40人
子木同
微信截图_20171127164950.png
image.png
image.png
image.png
image.png
https://dysms.console.aliyun.com/dysms.htm?spm=a3142.10677797.907839.sms5.73d623e172UAkJ#/account
SDK下载
https://help.aliyun.com/document_detail/55359.html?spm=5176.doc55496.6.581.33YXOb
微信截图_20171128112730.png image.pngroute.php
Route::get('test/testsend','api/test/testSend');
aliyun.php
<?php
/**
* 阿里云相关配置
* Created by PhpStorm.
* User: tong
* Date: 2017/11/29
* Time: 11:04
*/
return [
'appKey'=>'LTAIq8uO3D0kk2Ou',
'secretKey'=>'ajW116biRnnlS3E0JMP41PMpvKoyoG',
'signName'=>'tong娱乐app',
'templateCode'=>'SMS_105070029',
'identify_time'=>120,
];
Alidayu.php
<?php
/**
* Created by PhpStorm.
* User: tong
* Date: 2017/11/29
* Time: 10:57
*/
namespace app\common\lib;
use Aliyun\DySDKLite\Sms\SmsApi;
use think\Cache;
use think\Log;
/**
* 阿里大于发送短信基础类库
* Class Alidayu
* @package app\common\lib
*/
class Alidayu
{
const LOG_TPL='alidayu:';
/**
* 静态变量保存全局的实例
* @var null
*/
private static $_instance = null;
/**
* 私有的构造方法
* Alidayu constructor.
*/
private function __construct()
{
}
/**
* 静态方法 单例模式统一入口
*/
public static function getInstance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* 设置短信验证
* @param int $phone
* @return bool
*/
public function setSmsIdentify($phone = '0')
{
Log::write(self::LOG_TPL."setsms-start-set-----");
$code = rand(1000, 9999);
try {
$sms = new SmsApi(config('aliyun.appKey'), config('aliyun.secretKey')); // 请参阅 https://ak-console.aliyun.com/ 获取AK信息
$response = $sms->sendSms(
config('aliyun.signName'), // 短信签名
config('aliyun.templateCode'), // 短信模板编号
$phone, // 短信接收者
Array( // 短信模板中字段的值
"code" => $code,
"product" => "dsd"
),
"123" // 流水号,选填
);
//echo "发送短信(sendSms)接口返回的结果:\n";
/**
* stdClass Object
* (
* [Message] => OK
* [RequestId] => BD7636FA-CBBB-40E5-BFC8-82CA01987076
* [BizId] => 525010911925269941^0
* [Code] => OK
* )
*/
//print_r($response);
} catch (\Exception $e) {
//记录日志
Log::write(self::LOG_TPL."set-----", $e->getMessage());
return false;
}
if ($response->Code == 'OK') {
//设置验证码失效时间
Cache::set($phone, $code, config('aliyun.identify_time'));
return true;
}
//else {
Log::write(self::LOG_TPL."set-----" . json_encode($response));
//}
return false;
}
/**
* 根据手机号查询验证码是否正常
*/
public function checkSmsIdentify($phone)
{
if (!$phone) {
return false;
}
return Cache::get($phone);
}
}
Test.php
public function testSend()
{
$obj=Alidayu::getInstance()->setSmsIdentify('18617156713');
if($obj){
echo 'true';
}
}
image.png
image.png