阿里云短信验证使用(PHP)

1.登陆阿里云后台,事先添加签名和模板

阿里云短信验证使用(PHP)

2.使用composer下载阿里云SDK

composer require alibabacloud/sdk

在PHP7.0下安装需要提前安装curl扩展

sudo apt-get install php7.-curl

3. 提取短信发送配置到配置文件

return [
'accessKeyId' => 'LTAIMje******hS',
'accessSecret' => 'fMuQTLUrKQN******Z2m07',
'SignName' => '****',
'TemplateCode' => 'SMS_****04',
'regionId' => 'cn-hangzhou'
];

4. 新建阿里短信发送类

use Swoft\Task\Bean\Annotation\Task;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException; /**
* 阿里信息发送类
* @Task("ali")
*/
class AliTask
{
/**
* 发送短信验证码
*/
public function sendCode($phone,$code)
{
$config = \config('ali');
$param = [
'code' => $code
];
AlibabaCloud::accessKeyClient($config['accessKeyId'], $config['accessSecret'])
->regionId($config['regionId'])
->asGlobalClient(); try {
$result = AlibabaCloud::rpcRequest()
->product('Dysmsapi')
->version('2017-05-25')
->action('SendSms')
->method('POST')
->options([
'query' => [
'PhoneNumbers' => $phone,
'SignName' => $config['SignName'],
'TemplateCode' => $config['TemplateCode'],
'TemplateParam' => json_encode($param)
],
])
->request();
print_r($result->toArray());
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}
}
}
上一篇:node的http请求


下一篇:springMVC中 request请求数据绑定到Controller入参 过程剖析