PHP如何实现阿里云短信sdk灵活应用在项目中的方法
首先安装
composer require alibabacloud/sdk
接着看alibabacloud官网文档
https://packagist.org/packages/alibabacloud/sdk
示例
My_composer_aliyunsms.php
<?php // use Swoft\Task\Bean\Annotation\Task; // use AlibabaCloud\Client\AlibabaCloud; // use AlibabaCloud\Client\Exception\ClientException; // use AlibabaCloud\Client\Exception\ServerException; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; use AlibabaCloud\Ecs\Ecs; /** * 阿里信息发送类 * @Task("ali") */ class My_composer_aliyunsms { /** * 发送短信验证码 */ public function sendCode($config,$phone,$code) { $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(); return $result->toArray(); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; } } }
demo
include 'My_composer_aliyunsms.php'; $sms = new My_composer_aliyunsms(); $phone='xxxxxx'; $code='99999'; $config = [ 'accessKeyId' => 'LTAIMje******hS', 'accessSecret' => 'fMuQTLUrKQN******Z2m07', 'SignName' => '****', 'TemplateCode' => 'SMS_****04', 'regionId' => 'cn-hangzhou' ]; $re = $sms->sendCode($config,$phone,$code); var_dump($re);
相关推荐
CosEmon 2020-11-13
lihongtai 2020-11-09
sqisydream 2020-11-11
fjlmcy 2020-10-31
visionarywind 2020-10-31
特立独行 2020-10-30
Hitpoint云计算 2020-10-30
sqsfjsjlpf 2020-10-29
平凡的程序员 2020-10-15
yunna0 2020-09-23
OpenCDN网站加速 2020-09-21
82453961 2020-09-21
qual0 2020-09-21
mysteryrat 2020-09-21
Liucute 2020-09-21
古影轩 2020-09-17
aolia000 2020-09-11
罗忠浩 2020-08-16