legend3---PHP使用阿里云短信服务
legend3---PHP使用阿里云短信服务
一、总结
一句话总结:
使用步骤照官方文档,代码拷贝即可
1、php使用阿里云短信服务的步骤?
入驻阿里云->开通短信服务->获取AccessKey->创建签名和模版->短信接口配置->发送短信
2、lavarel使用阿里云短信服务需要注意的是?
1、使用按照官方文档一步步进行就行:地址为https://help.aliyun.com/document_detail/59210.html?spm=a2c4g.11186623.6.558.90af56e03gHGJk
2、代码官方有示例代码,不需要你写,拷贝代码替换accessKey相关参数即可
3、原理是阿里云根据你的accessKey找到你的阿里云账号,从而找到短信的签名和模板,短信的验证码我们自己指定即可
4、核心步骤是项目中composer安装阿里云客户端:composer require alibabacloud/client
3、lavarel使用阿里云短信服务 核心步骤是什么?
1、项目中composer安装阿里云客户端:composer require alibabacloud/client
2、将示例代码拷贝进项目,替换accessKey相关:
<?php use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; // Download:https://github.com/aliyun/openapi-sdk-php // Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>') ->regionId('cn-hangzhou') // replace regionId as you need ->asDefaultClient(); try { $result = AlibabaCloud::rpc() ->product('Dysmsapi') // ->scheme('https') // https | http ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host('dysmsapi.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "default", 'PhoneNumbers' => "11111111111", 'SignName' => "读书编程笔记", 'TemplateCode' => "SMS_172220636", 'TemplateParam' => "{\"code\":\"222\"}", ], ]) ->request(); print_r($result->toArray()); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; }
记得将<accessKeyId>和<accessSecret>替换为你自己的
二、PHP使用阿里云短信服务
1、使用按照官方文档一步步进行就行:地址为https://help.aliyun.com/document_detail/59210.html?spm=a2c4g.11186623.6.558.90af56e03gHGJk
2、项目代码示例:
<?php namespace App\Http\Controllers\Home\Login; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Gregwar\Captcha\CaptchaBuilder; //阿里云短信验证 use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; // Download:https://github.com/aliyun/openapi-sdk-php // Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md class RegController extends Controller { //获取手机验证码操作 public function phone_code(Request $request){ AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>') ->regionId('cn-hangzhou') // replace regionId as you need ->asDefaultClient(); try { $result = AlibabaCloud::rpc() ->product('Dysmsapi') // ->scheme('https') // https | http ->version('2017-05-25') ->action('SendSms') ->method('POST') ->host('dysmsapi.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "default", 'PhoneNumbers' => "11111111111", 'SignName' => "读书编程笔记", 'TemplateCode' => "SMS_172220636", 'TemplateParam' => "{\"code\":\"88888\"}", ], ]) ->request(); print_r($result->toArray()); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; } dd($request->all()); } }
核心代码不需要自己写,直接拷贝官方的代码,将下面等参数换成自己的即可,还有电话号码,短信的签名和模板等信息
'<accessKeyId>', '<accessSecret>'
3、发送结果



浙公网安备 33010602011771号