美文网首页
hyperf3.0中 集成阿里云短信发送sdk

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

作者: geeooooz | 来源:发表于2023-05-29 10:31 被阅读0次

参考网址: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);
    }

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

相关文章

  • 阿里云短信服务 报错NoClassDefFoundError

    好久没有撸代码了,突然想集成 阿里云短信服务玩玩。于是乎,按阿里云要求,提了短信签名和短信模板,然后就下载SDK集...

  • node 短信接口的调用

    首先安装一下 短信的sdk 依赖 这里使用的是阿里云的短信SDK,在阿里云官网申请 调用 这里新建个 messa...

  • 阿里云高级播放器集成

    相关链接:阿里云播放器SDK下载3.4.10阿里云播放器SDK集成文档查看 集成播放器的目的是让现有项目更好更方便...

  • 阿里云短信发送

    官方文档:https://help.aliyun.com/document_detail/101414.html?...

  • 阿里大于发送短信验证码demo

    使用阿里云的短信服务进行短信发送 1.在阿里的短信服务申请短信签名以及短信模板 短信签名:image.png 短信...

  • 使用阿里云的短信服务发送短信

    原文地址使用阿里云的短信服务发送短信 在给客户开发一个信息发送功能的时候,需要涉及到短信的发送,短信发送一般不同的...

  • SAP调用阿里云发送短信

    sap发送阿里云发送短信 完整程序引用 https://blog.csdn.net/xiefireworks/ar...

  • 阿里大于短信api集成

    阿里大于功能已集成到阿里云云通信里,重新整理下阿里大于短信功能 每条费用0.045元/条 一、进入阿里云短信平台开...

  • 用Laravel Sms实现 laravel短信验证码的发送

    阿里云短信服务使用Laravel Sms这个扩展包实现短信验证码的发送,这里以阿里云的短信服务为例:首先,要创建短...

  • python 阿里云短信发送

    简单的阿里云短信推送前提需要在阿里云短信服务中注册短信模板,通过审核,完成后记下签名名字,随后便可以引用一下代码进...

网友评论

      本文标题:hyperf3.0中 集成阿里云短信发送sdk

      本文链接:https://www.haomeiwen.com/subject/vtejedtx.html