開(kāi)通阿里云短信服務(wù)
登錄阿里云進(jìn)入控制臺(tái) 產(chǎn)品與服務(wù)-->云通訊-->短信服務(wù)-->國(guó)內(nèi)消息
- 創(chuàng)建 簽名與短信模板
輸入圖片說(shuō)明
-
創(chuàng)建 AccessKey
輸入圖片說(shuō)明
我這里使用子賬號(hào) 創(chuàng)建專門把短信服務(wù)授權(quán)給該賬號(hào)
輸入圖片說(shuō)明
輸入圖片說(shuō)明
創(chuàng)建完成后需要等待審核通過(guò)。
集成到Lumen 框架
官方PHP SKD Github : https://github.com/aliyun/openapi-sdk-php-client
安裝與使用
- 安裝
composer require alibabacloud/client
- 封裝在類庫(kù)
在Libs 目錄下 新建 Sms\AliyunSms.php 代碼如下
<?php
namespace App\Libs\Sms;
//阿里短信
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class AliyunSms
{
//阿里云accesskeys 授權(quán)
private $AccessKey; //AccessKeyID
private $AccessKeySecret; //AccessKeySecret
private $SignName; //短信簽名
/**
* AliyunSms constructor.
* @param array|null $config
*/
public function __construct(Array $config = null)
{
//此處需要替換成自己的AK信息
if ($config) {
$this->AccessKey = $config['AccessKey'];
$this->AccessKeySecret = $config['AccessKeySecret'];
$this->SignName = $config['SignName'];
} else {
$this->AccessKey = env('ALIYUN_ACCESSKEY');
$this->AccessKeySecret = env('ALIYUN_ACCESSKEYSECRET');
$this->SignName = env('ALIYUN_SMS_SIGNNAME');
}
}
/**
* 發(fā)送短信驗(yàn)證碼
* @param $mobile
* @param $code
* @param $templateCode
* @return string
* @throws ClientException
*/
public function sendCode($mobile,$code,$templateCode){
AlibabaCloud::accessKeyClient($this->AccessKey, $this->AccessKeySecret)
->regionId('cn-hangzhou') // replace regionId as you need
->asGlobalClient();
try {
$result = AlibabaCloud::rpcRequest()
->product('Dysmsapi')
// ->scheme('https') // https | http
->version('2017-05-25')
->action('SendSms')
->method('POST')
->options([
'query' => [
'SignName' => $this->SignName,
'PhoneNumbers' => $mobile,
'TemplateCode' => $templateCode,
'TemplateParam'=>'{"code":"'.$code.'"}'
],
])
->request()->toArray();
//返回字符串 ok 則發(fā)送成功
return $result['Message'];
} catch (ClientException $e) {
return $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
return $e->getErrorMessage() . PHP_EOL;
}
}
}
在配置文件 .env
中加入阿里云的配置
# 阿里云配置
ALIYUN_ACCESSKEY = xxxxxxxxx
ALIYUN_ACCESSKEYSECRET = xxxxxxxxxxxxxxxxxxxx
ALIYUN_SMS_SIGNNAME = xxxx
- 把短信驗(yàn)證碼功能添加為lumen服務(wù)提供者
創(chuàng)建文件 app\Providers\SmsServiceProvider.php
內(nèi)容如下
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Libs\Sms;
class SmsServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//注冊(cè)單例服務(wù)
$this->app->singleton('Sms\AliyunSms', function() {
return new Sms\AliyunSms();
});
}
/**
* Boot the authentication services for the application.
*
* @return void
*/
public function boot()
{
}
}
在控制器里面調(diào)用(不用引用類庫(kù) use App\Libs\Sms
忍宋,已經(jīng)注冊(cè)為服務(wù)提供者):
直接調(diào)用 App('Sms\AliyunSms')
$sms = App('Sms\AliyunSms');
$sendStr = $sms->sendCode('手機(jī)號(hào)碼','驗(yàn)證碼','阿里云短信模板code');
//注意為大寫(xiě)字母 OK
if($sendStr == 'OK'){
echo '發(fā)送成功';
} else {
echo $sendStr;
}
完成痕貌。