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.
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.
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.
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.
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.
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; }
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>") //短信模板替换参数,具体参数值由模板定义 );
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!