PHP Develops Practical Short Message Service

Step 1 Use Aliyun Short Message Service to Send Short Messages Normally

  • SMS signature
  • SMS template
  1. 1 Application for short message signature
    _Create signature information according to user attributes. Enterprise users need to upload certificates of relevant enterprise qualifications, and individual users need to upload certificates of personal identity.
    _Short message signatures need to be checked and approved before they can be used.

    1.2 Application for SMS Template
    _short message template, that is, the specific content of the message sent. The short message template can support four modes: authentication code, short message notification, promotion of short message and international/Hong Kong, Macao and Taiwan message. Verification code and short message notification, through variable replacement to achieve personalized short message customization. Promotion SMS does not support adding variables to templates.
    _SMS template needs to be audited before it can be used.

Step 2 Gets the Ali Cloud Access Key

Calling the SMS API also requires two parameters:

  • Access Key ID
  • Access Key Secret

_can pass through the Aliyun console. Key Management Page Create and manage all access key pairs and ensure that they are "enabled". Since the access key is the key factor for Aliyun to verify the security of API requests, please keep your access key properly. If some key pairs are at risk of leakage, it is recommended to delete the key pair in time and generate a new alternative key pair.

Step 3 Integrate Aliyun SMS into Laravel for development

  download The SDK provided by Aliyun develops and copies api_sdk/lib/Core and api_sdk/lib/Api into the project app/Tools/dysms. If not, you need to create it yourself.

Open the composer.json file in the project root directory, add the icon code to the classmap

Open the command line tool and enter in the project root directory:

composer dumpautoload  

Successful execution is shown below.

Next is the formal development, directly using the way of importing packages in the sdk to introduce:

The first postman test run reported errors as follows:

This is because in Endpointer Provider, getEndpoints are used directly without setEndpoints, so it returns empty information. The setEndpoints operation is called in EndpointConf, loading endpoints in xml and adding them to the calling code.

// Manual loading endpoint

Running the code again found the following error:

This is because of the proxy. Just comment on the code.

Develop complete code. github source address:


namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Core\Regions\EndpointConfig;

class Dysms extends Controller
    static $acsClient = null;

    //Ali SMS
    public function Dysms()
        // Initialize SendSmsRequest instance to set parameters for sending short messages
        $request = new SendSmsRequest();

        //Optional - Enable https protocol

        // Mandatory, Set Short Message Receiving Number

        // The name of the signature should be set strictly according to the name of the signature. Please refer to

        // To set the template CODE, you should strictly follow the "template CODE" to fill in. Please refer to:
        $request->setTemplateCode("Template CODE");

        // Optionally, set template parameters. If there are variables in the template that need to be replaced, it is mandatory.
            // The Value of the Field in the Short Message Template
            "code" => rand(000000, 999999),
            "product" => "dsd"

        // Optionally, set the pipelining number

        // Selected, Uplink SMS Extension Code (Extension Code field is controlled at or below 7 bits, please ignore this field for users without special needs)
//        $request->setSmsUpExtendCode("1234567");
        // Initiate access requests
        $acsResponse = static::getAcsClient()->getAcsResponse($request);
//Print execution results
// Return status after successful execution
//        {#1538
//            +"Message": "OK"
//            +"RequestId": "0CE56BFE-5E9A-4F3F-8AEC-5C524720CE83"
//            +"BizId": "480519725607521112^0"
//            +"Code": "OK"
//        }

     * Get AcsClient
     * @return DefaultAcsClient
    public static function getAcsClient()
        //Product Name: Cloud Communication Traffic Service API product, developers do not need to replace
        $product = "Dysmsapi";
        //Product domain name, developers do not need to replace
        $domain = "";
        // TODO needs to be replaced here with the developer's own AK (
        // AccessKeyId
        $accessKeyId = "AccessKeyId";
        // AccessKeySecret
        $accessKeySecret = "AccessKeySecret";
        // Multiple Region s are not supported for the time being
        $region = "cn-hangzhou";
        // Service node
        $endPointName = "cn-hangzhou";
        if (static::$acsClient == null) {
            //Initialization of acsClient does not support region ization for the time being
            $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
            // Manual loading endpoint
            // Increasing service nodes
            DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);
            // Initialize AcsClient to initiate requests
            static::$acsClient = new DefaultAcsClient($profile);
        return static::$acsClient;

PS: finally had to Tucao down, Ali used OSS and Ali SMS lot of pit ah, such a big company.

Tags: PHP SDK github Laravel JSON

Posted on Mon, 24 Dec 2018 18:33:06 -0800 by lemist