PHP对接阿里云最新版短信接口

获取示例代码使用:

https://api.aliyun.com/new#/?product=Dysmsapi&version=2017-05-25&api=SendSms¶ms={%22RegionId%22:%22cn-hangzhou%22}&tab=DEMO&lang=PHP

 

SDK下载地址

 

https://help.aliyun.com/document_detail/53111.html?spm=a2c1g.8271268.10000.132.596edf25nMDlN9

 

1、下载SDK

 

PHP对接阿里云最新版短信接口_第1张图片

SDK下载完后是这个样子,把vendor改为你自己的命名,然后引入autoload.php

require __DIR__ . '/aliyunsms/autoload.php';

同时引用这3个

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

2、具体代码

/**
     * 发送验证码
     * @param tel 手机号
     * @param code 验证码
     */
    function send_sms_code($tel,$code)
    {
        AlibabaCloud::accessKeyClient('AccessKey ID', 'AccessKey Secret')
            ->regionId('cn-hangzhou')
            ->asDefaultClient();

        $TemplateParam['code'] = $code;
        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' => "cn-hangzhou",
                        'PhoneNumbers' => '手机号',
                        'SignName' => "签名名称",
                        'TemplateCode' => "模板ID",
                        'TemplateParam' => '短信模板变量对应的实际值,JSON格式  例:{"code":"1111"}',
                    ],
                ])
                ->request();
            $obj = $result->toArray();
            if($obj['Message'] == 'OK'){
                return true;
            }else{
                return false;
            }
        } catch (ClientException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        } catch (ServerException $e) {
            echo $e->getErrorMessage() . PHP_EOL;
        }
    }

 

3、Message值为OK代表成功,若不成功参照错误码查找原因,错误码链接https://help.aliyun.com/document_detail/101346.html?spm=a2c4g.11186623.2.14.22cb56e00jVdMY

4、返回参数的BizId为发送回执ID,可根据该ID在接口QuerySendDetails中查询具体的发送状态。

到这就完事了  有问题可以留言~

 

注: 个人原创 转载请标注原文链接!

 

你可能感兴趣的:(PHP对接阿里云最新版短信接口)