PHP code example of seffeng / sms

1. Go to this page and download the library: Download seffeng/sms library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

seffeng / sms example snippets


/**
 * SiteController
 */
use Seffeng\Sms\SmsClient;
use Seffeng\Sms\Exceptions\SmsException;

class SiteController extends Controller
{
    public function index()
    {
        try {
            $sdkAppId = '';             // 腾讯云SDKAppID  阿里云不需要
            $appSecretId = '';          // 腾讯云SecretId 或 阿里云 AccessKeyId
            $appSecretKey = '';         // 腾讯云SecretKey 或 阿里云 AccessKeySecret
            $name = 'aliyun';           // qcloud 或 aliyun
            $signName = '签名';         // 签名内容
            $tempCode = 'SMS_153055065'; // 腾讯云 templateId[1234] 或 阿里云 TemplateCode[SMS_153055065]
            $content = ['111111'];      // 腾讯云 ['111111'] 或 阿里云 ['code' => '111111']
            $phone = '13800138000';     // 相同内容可批量发送['13800138000', '13800138001']

            // 因阿里云与腾讯云的内容参数结构不一致,参考 $content;可通过 TemplateParams 实现以腾讯云结构发送
            $templateParamsModel = new TemplateParams();
            $client = new SmsClient($appSecretId, $appSecretKey, $sdkAppId);
            $result = $client->setClient($name, $templateParamsModel)
                        ->setSignName($signName)
                        ->setTemplateCode($tempCode)
                        ->send($phone, $content);

            if ($result) {
                echo '发送成功!';
            } else {
                echo '发送失败!';
            }
        } catch (SmsException $e) {
            echo $e->getMessage();
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }
}

/**
 * TemplateParams
 * @see Seffeng\Sms\Clients\Aliyun\TemplateParams
 */
class TemplateParams extends \Seffeng\Sms\Clients\Aliyun\TemplateParams
{
    /**
     * 重写模板对应参数
     * @return array
     */
    public static function fetchNameItems()
    {
        return [
            'SMS_153055065' => ['code'],
            'SMS_153055066' => ['code', 'address'],
        ];
    }
}

│ SmsClient.php                 基类
├─Clients
│  ├─Aliyun                     阿里云
│  │      Client.php                阿里云发送处理类
│  │      Error.php                 阿里云错误
│  │      TemplateParams.php        短信内容参数对应
│  └─Qcloud                     腾讯云
│          Client.php               腾讯云发送处理类
│          Error.php                腾讯云错误
├─Exceptions
│      SmsException.php             异常基类
└─Helpers
       ArrayHelper.php              数组帮助类