Home > Backend Development > PHP Tutorial > PHP calls the Qixintong SMS interface to send SMS messages

PHP calls the Qixintong SMS interface to send SMS messages

WBOY
Release: 2023-05-22 18:02:01
Original
1277 people have browsed it

Qixintong is currently one of the largest enterprise-level SMS service providers in China. Its SMS interface is simple and easy to use and is loved by the majority of enterprise users. This article will introduce how to use PHP to call the Qixintong SMS interface to send SMS messages.

1. Apply for an enterprise SMS interface account

First, we need to apply for an enterprise SMS interface account on the official website of Qixintong. Fill in the relevant company information according to the prompts on the official website, and after passing the review, you can log in to the console and obtain API-key, API-secret and other credential information.

2. Call Qixintong SMS interface

Qixintong provides interface sample codes in multiple languages. This article uses PHP as an example to introduce how to call the SMS interface to send SMS messages.

First, we need to define the SMS sending interface address and request parameters:

$url='http://api.qxtsms.com/sendSms.do';  //短信接口地址
$params=array(
    'apiKey'=>'**********',  //API Key
    'apiSecret'=>'**********',  //API Secret
    'templateId'=>123456,  //短信模板ID
    'mobile'=>'**********',  //手机号码,多个号码以英文逗号隔开
    'templateParams'=>'参数1,参数2'  //短信模板参数,多个参数以英文逗号隔开
);
Copy after login

Among them, the API Key and API Secret are the credential information obtained when applying for an account, and the templateId is already in Qixintong The SMS template ID created in the background, mobile is the mobile phone number that receives the SMS, templateParams is the replacement parameters in the SMS template, and multiple parameters are separated by English commas.

Next, we use curl to send a POST request to call the SMS interface:

$curl=curl_init();  //初始化curl
curl_setopt($curl,CURLOPT_URL,$url);  //设置请求地址
curl_setopt($curl,CURLOPT_POST,true);  //设置请求类型为POST
curl_setopt($curl,CURLOPT_POSTFIELDS,http_build_query($params));  //设置请求参数
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);  //设置返回结果为字符串
$result=curl_exec($curl);  //执行请求,并获取返回结果
curl_close($curl);  //关闭curl句柄
Copy after login

Among them, the http_build_query function is used to convert the request parameters into a URL-encoded string form for transmission in the POST request. .

3. Processing SMS sending results

The results returned by the SMS sending interface are in JSON format, and we need to parse them and determine the sending results. As shown below:

$response=json_decode($result,true);  //将JSON格式转换为关联数组
if($response['code']=='0'){  //判断发送结果
    echo '短信发送成功!';
}else{
    echo '短信发送失败:'.$response['errorMsg'];
}
Copy after login

Among them, the basis for judging the sending result is the value of the code field. If it is 0, it means the sending is successful, otherwise it means the sending fails, and the errorMsg field is the reason for the sending failure.

To sum up, it is very simple to use PHP to call the Qixintong SMS interface to send SMS messages. You only need to apply for an account, set the request parameters, call the SMS interface and process the sending results. However, it should be noted that Qixintong’s SMS interface has sending limit and template review requirements, and we need to strictly abide by relevant regulations.

The above is the detailed content of PHP calls the Qixintong SMS interface to send SMS messages. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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