Home > Backend Development > PHP Tutorial > PHP calls Huawei Cloud SMS interface to send SMS messages

PHP calls Huawei Cloud SMS interface to send SMS messages

WBOY
Release: 2023-05-26 20:22:02
Original
1806 people have browsed it

With the popularity of smart phones, text messaging has become an indispensable part of people's lives. Whether for business or personal use, text messaging is an important form of communication. Huawei Cloud is a leading cloud computing service provider. In order to meet user needs, Huawei Cloud provides SMS services. In this article, we will introduce how to use PHP to call the Huawei Cloud SMS interface to send SMS messages.

  1. Register a Huawei Cloud account

First, you need to register a Huawei Cloud account and activate the SMS service. If you already have a Huawei Cloud account, you can directly enter the console to activate the SMS service.

  1. Create application and signature

In the console, enter the SMS module, click "Application Management" in the left menu, then click "Create Application" and fill in the application Name and description. Next, create a signature, which is used to identify the source of the text messages you send.

  1. Create Template

In the SMS module, click "Template Management" and then click "Create Template". When creating a template, you need to fill in the template name, template content, and review instructions.

  1. Get the API calling address

Huawei Cloud provides API interfaces for users to call. In the console, go to the API Management menu and select Call Address. You can see information such as the API's URL address and access key.

  1. PHP code to implement SMS sending

Next, enter the PHP code to implement SMS sending.

First, create a sendSms() function for sending text messages. Three parameters need to be passed: $accessKeyId, $accessKeySecret, $params.

$accessKeyId and $accessKeySecret can be obtained from the API management of Huawei Cloud. $params contains the necessary parameters of the text message, such as mobile phone number, signature, template ID, etc. The code is as follows:

function sendSms($accessKeyId, $accessKeySecret, $params) {
    $url = "https://api.<region>.myhuaweicloud.com/sms/batchSendSms/v1"; //API接口地址
    $headers = array(
        "Content-type: application/json;charset=utf-8", //请求Body数据格式
        "X-WSSE: xxxxxx"  //使用API密钥认证
    );
    $accessKey = base64_encode($accessKeyId . ':' . $accessKeySecret);
    array_push($headers, "Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"",
    "X-WSSE: UsernameToken Username="{$accessKeyId}",PasswordDigest="{$accessKey}",Nonce="xxxxxxxxxxx",Created="xxxxxxxxxxx""); //使用API密钥认证,请将参数替换为实际值
    $params_str = json_encode($params); //参数转化为json格式
    $ch = curl_init($url); //初始化curl
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS,$params_str); //请求Body部分
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}
Copy after login

Before calling the sendSms() function, you need to organize the parameters first. The sample code is as follows:

$params = array(
    "from" => "<签名名称>", //短信签名,从控制台中获取
    "to" => "<手机号码>", //接收短信的手机号码
    "templateId" => "<模板ID>", //短信模板ID,从控制台中获取
    "templateParas" => array("<参数1>", "<参数2>") //短信模板替换参数,具体参数值由模板定义
);
Copy after login

At this point, the code for PHP to call the Huawei Cloud SMS interface to send SMS messages has been completed. In actual use, the corresponding parameters need to be replaced.

Summary

This article introduces how to use PHP to call the Huawei Cloud SMS interface to send SMS messages. It should be noted that each time a text message is sent, a certain number of API calls will be consumed, so the number of API calls needs to be used reasonably according to actual needs. The code in this article is only a sample code, and the specific implementation needs to be adjusted according to the actual situation.

The above is the detailed content of PHP calls Huawei Cloud SMS interface to send SMS messages. 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