Home > Backend Development > PHP Tutorial > How to use PHP to connect to Alibaba Cloud SMS service to implement marketing SMS sending function

How to use PHP to connect to Alibaba Cloud SMS service to implement marketing SMS sending function

王林
Release: 2023-07-06 08:12:01
Original
1651 people have browsed it

How to use PHP to connect to Alibaba Cloud SMS service to implement marketing SMS sending function

Introduction:
With the advent of the mobile Internet era, SMS, as a fast and efficient communication method, has become the first choice for merchants to carry out marketing. One of the important means. Alibaba Cloud SMS Service is one of the most well-known SMS platforms in China and provides developers with a rich API. This article will introduce how to use PHP to connect to Alibaba Cloud SMS Service to implement the marketing SMS sending function.

1. Preparation

  1. Register an Alibaba Cloud account and activate the Alibaba Cloud SMS service;
  2. Configure the local PHP environment to ensure that PHP programs can run normally;
  3. Download the SDK of Alibaba Cloud SMS Service.

2. Install SDK

  1. Download SDK compression from the official website of Alibaba Cloud SMS Service (https://help.aliyun.com/document_detail/55451.html) Package;
  2. Unzip the SDK compressed package and copy the SDK folder to the root directory of the project.

3. Write code
After completing the preparation work and installing the SDK, we can start writing PHP code.

  1. Introduce SDK
require_once "SDK路径/aliyun-php-sdk-core/Config.php";
require_once "SDK路径/aliyun-php-sdk-dysmsapi/Config.php";
Copy after login
  1. Create SMS sending class
use DysmsapiRequestV20170525SendSmsRequest;
class SmsSender
{
    private $accessKeyId;           // 阿里云短信服务的Access Key ID
    private $accessKeySecret;       // 阿里云短信服务的Access Key Secret
    private $signName;              // 短信签名
    private $templateCode;          // 短信模板ID
    private $product;               // 产品名称

    public function __construct($accessKeyId, $accessKeySecret, $signName, $templateCode, $product)
    {
        $this->accessKeyId = $accessKeyId;
        $this->accessKeySecret = $accessKeySecret;
        $this->signName = $signName;
        $this->templateCode = $templateCode;
        $this->product = $product;
    }

    public function sendSms($phoneNumber, $templateParam)
    {
        $iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $this->accessKeyId, $this->accessKeySecret);
        DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
        $client = new DefaultAcsClient($iClientProfile);

        $request = new SendSmsRequest;
        $request->setPhoneNumbers($phoneNumber);
        $request->setSignName($this->signName);
        $request->setTemplateCode($this->templateCode);
        $request->setTemplateParam(json_encode($templateParam));
        $request->setOutId($this->product);

        try {
            $response = $client->getAcsResponse($request);
            return $response;
        } catch (Exception $e) {
            return $e;
        }
    }
}
Copy after login

4. Use Alibaba Cloud SMS service to send SMS messages

$accessKeyId = "你的Access Key ID";
$accessKeySecret = "你的Access Key Secret";
$signName = "你的短信签名";
$templateCode = "你的短信模板ID";
$product = "你的产品名称";

$smsSender = new SmsSender($accessKeyId, $accessKeySecret, $signName, $templateCode, $product);

$phoneNumber = "接收短信的手机号码";
$templateParam = [
    "code" => "123456"  // 模板中的变量替换值
];

$response = $smsSender->sendSms($phoneNumber, $templateParam);

if ($response->Code == "OK") {
    echo "短信发送成功!";
} else {
    echo "短信发送失败:" . $response->Message;
}
Copy after login

The above are the detailed steps and sample code for using PHP to connect to Alibaba Cloud SMS service to implement the marketing SMS sending function. I hope this article can help everyone. If you have any questions, please leave a message for discussion. I wish you all the best with your marketing campaigns!

The above is the detailed content of How to use PHP to connect to Alibaba Cloud SMS service to implement marketing SMS sending function. For more information, please follow other related articles on the PHP Chinese website!

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