PHP 接入阿里云短信/语音接口

首先要安装阿里云的依赖包 以下代码仅供参考

下面->action中的方法就是阿里云文档中对应的方法

<?php

namespace App\Http\Controllers\Plugin;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use Illuminate\Support\Facades\Log;

class Send
{
    protected $CalledNumber;
    protected $templateCode;
    protected $params;

    public function __construct($CalledNumber,string $templateCode,string $params)
    {
        $this->CalledNumber = $CalledNumber;
        $this->templateCode = $templateCode;
        $this->params = $params;
    }

    /**
     *  发送短信
     * @return array|void
     * @throws ClientException
     */
    public function sendSms(){
         AlibabaCloud::accessKeyClient(config('aliyun.access_key_id'), config('aliyun.access_key_secret'))
            ->regionId(config('aliyun.region_id'))//设置地域ID
            ->asDefaultClient();

        try {
            //SendSms短信通知
            $product = 'Dysmsapi';
            $paramsKey = 'TemplateParam';
            $query = [
                'RegionId' => config('aliyun.region_id'),
                'PhoneNumbers' => $this->CalledNumber,
                'SignName' => config('aliyun.call_sb_sign_name'),
                'TemplateCode' => $this->templateCode,
            ];

            if($this->params){
                $this->params = [$paramsKey=>$this->params];
                $query = array_merge($query,$this->params);
            }
            $result = AlibabaCloud::rpc()
                ->product($product)
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host($product.'.aliyuncs.com')
                ->options([
                    'query' => $query,
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException $e) {
            Log::error($e->getMessage(),['uri'=>\request()->path()]);
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            Log::error($e->getMessage(),['uri'=>\request()->path()]);
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

    /**
     *  发送语音
     *
     * @param $argv
     * @return array|\Illuminate\Http\JsonResponse
     * @throws ClientException
     */
    public static function sendVoice($argv)
    {
        AlibabaCloud::accessKeyClient(config('aliyun.access_key_id'), config('aliyun.access_key_secret'))
            ->asDefaultClient();

        try {
            $product = 'dyvmsapi';
            $query = [
                'CalledNumber' => $argv['CalledNumber'],//要发送的手机号
                'TtsCode' => $argv['TtsCode'],//语音模板
            ];
            $result = AlibabaCloud::rpc()
                ->product($product)
                ->version('2017-05-25')
                ->action('SingleCallByTts')
                ->method('POST')
                ->host($product.'.aliyuncs.com')
                ->options([
                    'query' => $query,
                ])
                ->request();
            return $result->toArray();
        } catch (ClientException|ServerException $e) {
            Log::error($e->getMessage(),['uri'=>\request()->path()]);
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答你的问题。接入腾讯云短信API需要以下几个步骤: 1. 注册腾讯云账号,并开通短信服务。 2. 创建应用,获取 AppID 和 AppKey。 3. 安装腾讯云 SDK。 4. 在 ThinkPHP5 中集成腾讯云 SDK。 5. 编写发送短信的代码。 以下是详细步骤: 1. 注册腾讯云账号,并开通短信服务。 首先,你需要注册一个腾讯云账号,然后在控制台中开通短信服务。在开通短信服务时,你需要填写企业信息并进行实名认证。 2. 创建应用,获取 AppID 和 AppKey。 在腾讯云控制台中创建应用,并获取 AppID 和 AppKey。这些信息将在后面的代码中使用。 3. 安装腾讯云 SDK。 在你的项目中安装腾讯云 SDK。可以通过 Composer 安装,也可以手动下载安装。 4. 在 ThinkPHP5 中集成腾讯云 SDK。 在 ThinkPHP5 中集成腾讯云 SDK,可以在 config.php 配置文件中添加以下配置: ```php // config.php return [ 'qcloud' => [ 'sms' => [ 'app_id' => 'your-app-id', 'app_key' => 'your-app-key', 'template_id' => 'your-template-id', 'sms_sign' => 'your-sms-sign', ], ], ]; ``` 上面的配置中,app_id 和 app_key 是在第二步中获取的,template_id 是短信模板的 ID,sms_sign 是短信签名。 5. 编写发送短信的代码。 在 ThinkPHP5 中发送短信可以通过继承 Qcloud\Sms\SmsSingleSender 类来实现。以下是发送短信的示例代码: ```php use Qcloud\Sms\SmsSingleSender; $appId = config('qcloud.sms.app_id'); $appKey = config('qcloud.sms.app_key'); $templateId = config('qcloud.sms.template_id'); $smsSign = config('qcloud.sms.sms_sign'); $sender = new SmsSingleSender($appId, $appKey); $result = $sender->sendWithParam('86', $mobile, $templateId, $params, $smsSign); if ($result === false) { return '发送短信失败'; } $response = json_decode($result, true); if ($response['result'] !== 0) { return '发送短信失败:' . $response['errmsg']; } return '发送短信成功'; ``` 上面的代码中,$mobile 是接收短信的手机号码,$params 是短信模板中的参数,$result 是发送短信的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值