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

PHP calls the Qixintong SMS interface to send SMS messages

WBOY
WBOYOriginal
2023-05-22 18:01:361267browse

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'  //短信模板参数,多个参数以英文逗号隔开
);

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句柄

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'];
}

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!

Statement:
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