赶知识网

hyperf3.0中 集成阿里云短信发送sdk

2023-06-26 / 389次点击 php/mysql/apache hyperf

参考网址:https://next.api.aliyun.com/api-tools/sdk/Dysmsapi?version=2017-05-25&language=php-tea

1:https://blog.csdn.net/TiaoZhanJi_Xian/article/details/121162541

2:https://blog.csdn.net/weixin_43811134/article/details/109672223


我用的是1方案。


环境要求

最低要求 PHP 5.6

安装 SDK 核心库 OpenAPI ,如果已在系统上全局安装 Composer,请直接在项目目录中运行以下内容来安装 Alibaba Cloud SDK for PHP 作为依赖项:(我就是没做这一步,咔咔报错)

1.composer require alibabacloud/darabonba-openapi


一些用户可能由于网络问题无法安装,可以通过以下命令使用阿里云 Composer 全量镜像。


composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

3.最后安装对应的包

composer require alibabacloud/dysmsapi-20170525


接下来是代码部分:

1.新建Service


<?php



namespace App\Service;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;

use Darabonba\OpenApi\Models\Config;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;


class AliSmsService

{

    const KEY = '在这儿写自己的';

    const SECRET = '在这儿写自己的';

    const SIGN = '写自己的signname';

    /**

     * 使用AK&SK初始化账号Client

     * @param mixed $accessKeyId

     * @param mixed $accessKeySecret

     * @return Dysmsapi Client

     */

    public static function createClient($accessKeyId = null, $accessKeySecret = null)

    {

        $config = new Config([

            "accessKeyId" => $accessKeyId ?? AliSmsService::KEY,

            "accessKeySecret" => $accessKeySecret ?? AliSmsService::SECRET

        ]);

        // 访问的域名

        $config->endpoint = "dysmsapi.aliyuncs.com";

        return new Dysmsapi($config);

    }


    /**

     * 短信验证码

     * @param int $phone 手机号码

     * @param int $code 验证码

     * @return array

     */

    public static function verify(int $phone, int $code,String $templateCode='')

    {

        $client = self::createClient(AliSmsService::KEY, AliSmsService::SECRET);

        $sendSmsRequest = new SendSmsRequest([

            "phoneNumbers" => $phone,

            "signName" => AliSmsService::SIGN,

            "templateCode" => $templateCode ? : config('sms.templateCode'),//这里是短信的模板id

            "templateParam" => json_encode([

                'code' => $code

            ])

        ]);

        $result = $client->sendSms($sendSmsRequest);

        if ($result->body->message == 'OK' && $result->body->code == 'OK') {

            return ['status' => 1];

        }

        if ($result->body->code == 'isv.MOBILE_NUMBER_ILLEGAL') {

            return ['status' => 0, 'msg' => '手机号码格式不正确'];

        }

        return ['status' => 0, 'msg' => '短信发送失败,网络繁忙'];

    }

}

控制器中:


use App\Service\AliSmsService;


#[Inject]

private AliSmsService $aliSmsService;//阿里云短信服务



/**

     * User: wujiawei

     * DateTime: 2023/5/30 10:06

     * describe:短信发送

     * @return array|int[]|string

     */

    #[GetMapping(path: "sendcode")]

    public function sendcode(){

        if($this->request->has('phone')){

            $phone = $this->request->input('phone');

            $code = rand(100000, 999999);

            //发送短信

            $result = $this->aliSmsService->verify($phone, $code);

        }else{

            $result = [

                'code'=>400,

                'message'=>'参数不完整'

            ];

        }

        return $this->response->json($result);

    }

基本上就是这些 ,有问题再自己查吧。


作者:geeooooz

链接:https://www.jianshu.com/p/2cc9bb6df2b5

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


有用 没用

Top10

沪ICP备09053415号 © 赶知识网