THINKPHP5 Integrated Aliyun SMS

Source: Internet
Author: User
Tags aliyun

First go to Aliyun application account (background address: https://www.aliyun.com/, note is not more than Ali SMS platform, Ali is more than has stopped the new user registration), login to the console, to create keyID and Keysecret.
Download SDK from Aliyun SMS website (https://help.aliyun.com/document_detail/55359.html?spm=5176.8195934.507901.12.2NiZni)
Place the SDK in the vendor directory of the THINKPHP5 framework, as shown in the following figure:

Send SMS code 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 '];//aliyun sms keyID $accessKeySecret = $this->config[' Sms_secretkey '];//
        Aliyun SMS Keysecret//SMS API Product Name $product = "Dysmsapi";
        SMS API product Domain name $domain = "Dysmsapi.aliyuncs.com";

        Region $region = "Cn-hangzhou" is not supported at the moment;
        Initialize access to the 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");/required-SMS Receive number $request->setsignname ("xxxx");/required-Short message signature: How to add a signature can refer to Aliyun SMS or T Pshop Official Document//required-SMS template code $requeSt->settemplatecode ("Modify mobile phone number");/required-Short message signature: How to add a signature can refer to Aliyun SMS or Tpshop Official document/optional-required (JSON format) if there are variables in the template that need to be replaced $req

        Uest->settemplateparam ("{\" code\ ": \" 4894\ "}");//SMS Signature content://optional-send SMS Serial number//$request->setoutid ("1234");

        Initiating an access request $RESP = $acsClient->getacsresponse ($request);  SMS sent successfully returns TRUE, Failure returns False if ($resp && $resp->code = = ' OK ') {return array (' status ' => 1,
        ' msg ' => $resp->code); else {return array (' status ' =>-1, ' msg ' => $resp->message. ' Subcode: '.
        $resp->code);
 }
    }

Note that you must add "./" to the SDK file that references Aliyun SMS, or you may not find the PHP file.
After integration, the test results are as follows

For full demo download address, please go to tpshop website to download the open source version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.