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

PHP calls the Yunpian SMS interface to send SMS messages

王林
王林Original
2023-05-22 08:03:051383browse

In today's mobile Internet era, text messaging has become a convenient and practical way of communication, widely used in all walks of life. The Yunpian SMS interface provides a fast, reliable and secure SMS service platform for enterprises and individuals. Its API interface is simple and easy to use, which greatly facilitates the integration operation between users and the system. This article will introduce how to use PHP to call the Yunpian SMS interface to realize the function of sending SMS.

1. Register a Yunpian SMS platform account

First, you need to register a Yunpian SMS platform account, activate and recharge the account, and then create a new API Key. After registering on the official website, enter the console and select "Account Management" - "API Key" - "Create API Key" to obtain an API Key for subsequent API call verification.

2. Install the PHP network request library

PHP needs to use the network request library to send text messages. Here we use GuzzleHttp. Use Composer to install GuzzleHttp:

composer require guzzlehttp/guzzle

3. Write the PHP code for sending text messages

The following is a PHP call to cloud slices The SMS interface implements the sample code for sending SMS:

<?php

require 'vendor/autoload.php';

// 设置 API Key
$apikey = 'YOUR_APIKEY';

// 设置要发送的手机号和短信内容
$mobile = 'YOUR_MOBILE';
$text = '【签名】您的验证码是1234';

// 调用接口发送短信
$client = new GuzzleHttpClient();
$response = $client->request(
    'POST',
    'https://sms.yunpian.com/v2/sms/single_send.json',
    [
        'form_params' => [
            'apikey' => $apikey,
            'mobile' => $mobile,
            'text' => $text
        ]
    ]
);

// 解析响应结果
$body = $response->getBody();
$json = json_decode($body, true);
$code = $json['code'];
$msg = $json['msg'];

if ($code == 0) {
    echo '发送成功';
} else {
    echo "发送失败:$msg";
}

In the code, we first set the API Key, and then set the mobile phone number and SMS content to be sent. Then, use GuzzleHttp to send a POST request and pass the API Key, mobile phone number, and SMS content to the Yunpian SMS interface. Finally, parse the response result to determine whether the transmission was successful.

4. Sending template text messages

In actual development, we may need to send template text messages instead of ordinary text messages. The Yunpian SMS interface supports sending template SMS. You need to create an SMS template in the console and specify the template ID. The following is an example of PHP code for sending template SMS:

<?php

require 'vendor/autoload.php';

// 设置 API Key
$apikey = 'YOUR_APIKEY';

// 设置要发送的手机号和模板 ID
$mobile = 'YOUR_MOBILE';
$tpl_id = 'YOUR_TEMPLATE_ID';
$param = '您的验证码是1234';

// 调用接口发送短信
$client = new GuzzleHttpClient();
$response = $client->request(
    'POST',
    'https://sms.yunpian.com/v2/sms/tpl_single_send.json',
    [
        'form_params' => [
            'apikey' => $apikey,
            'mobile' => $mobile,
            'tpl_id' => $tpl_id,
            'tpl_value' => urlencode("#code#=$param")
        ]
    ]
);

// 解析响应结果
$body = $response->getBody();
$json = json_decode($body, true);
$code = $json['code'];
$msg = $json['msg'];

if ($code == 0) {
    echo '发送成功';
} else {
    echo "发送失败:$msg";
}

Among them, we set the mobile phone number, template ID and template parameters to be sent, use urlencode to encode the template parameters, and then call the interface to send the SMS. It should be noted that the format of template parameters is "#variable name#=value", such as "#code#=1234".

The above is the basic process and sample code for using PHP to call the Yunpian SMS interface to send text messages. Through the introduction and practice of this article, I believe that readers have mastered the skills and methods of how to use the Yunpian SMS interface to send text messages, and can better develop text message functions suitable for their own projects.

The above is the detailed content of PHP calls the Yunpian 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