Home > Backend Development > PHP Tutorial > aliyun SMS interface development example, thkinkphp SMS interface development example

aliyun SMS interface development example, thkinkphp SMS interface development example

PHP中文网
Release: 2023-03-15 17:18:02
Original
2219 people have browsed it

For the complete demo download address, please go to TPshop official website to download.
Apply for SMS key, SMS signature and add SMS template (the specific adding method can be found on Alibaba. For SMS, please refer to the official document)
Put the SDK into the thinkphp5 framework Under the vendor directory, as shown below:

aliyun SMS interface development example, thkinkphp SMS interface development example

The code to send SMS is as follows:

private function sendSmsByAliyun()
    {
        include_once './vendor/aliyun-php-sdk-core/Config.php';
        include_once './vendor/Dysmsapi/Request/V20170525/SendSmsRequest.php';
        $accessKeyId = $this->config['sms_appkey'];//阿里云短信keyId
        $accessKeySecret = $this->config['sms_secretKey'];//阿里云短信keysecret
        //短信API产品名
        $product = "Dysmsapi";
        //短信API产品域名
        $domain = "dysmsapi.aliyuncs.com";
        //暂时不支持多Region
        $region = "cn-hangzhou";
        //初始化访问的acsCleint
        $profile = \DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
        \DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
        $acsClient= new \DefaultAcsClient($profile);
        $request = new \Dysmsapi\Request\V20170525\SendSmsRequest;
        $request->setPhoneNumbers("138xxx");//必填-短信接收号码
        $request->setSignName("xxxx");//必填-短信签名: 如何添加签名可以参考阿里云短信或TPshop官方文档
        //必填-短信模板Code
        $request->setTemplateCode("修改手机号码");//必填-短信签名: 如何添加签名可以参考阿里云短信或TPshop官方文档
        //选填-假如模板中存在变量需要替换则为必填(JSON格式)
        $request->setTemplateParam("{\"code\":\"4894\"}");//短信签名内容:
        //选填-发送短信流水号
        //$request->setOutId("1234");
        //发起访问请求
        $resp = $acsClient->getAcsResponse($request);
        //短信发送成功返回True,失败返回false
        if ($resp && $resp->Code == 'OK') {
            return array('status' => 1, 'msg' => $resp->Code);
        } else {
            return array('status' => -1, 'msg' => $resp->Message . ' subcode:' . $resp->Code);
        }
    }
Copy after login

Note, add "./" before referencing the SDK file of Alibaba Cloud SMS. , otherwise the PHP file may not be found.
After integration, the test results are as follows

aliyun SMS interface development example, thkinkphp SMS interface development example

aliyun SMS interface development example, thkinkphp SMS interface development example


##

The above is the detailed content of aliyun SMS interface development example, thkinkphp SMS interface development example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template