PHP code example of 888hou / easy-sms

1. Go to this page and download the library: Download 888hou/easy-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/ */

    

888hou / easy-sms example snippets


use Overtrue\EasySms\EasySms;

$config = [
    // HTTP 请求的超时时间(秒)
    'timeout' => 5.0,

    // 默认发送配置
    'default' => [
        // 网关调用策略,默认:顺序调用
        'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

        // 默认可用的发送网关
        'gateways' => [
            'yunpian', 'aliyun',
        ],
    ],
    // 可用的网关配置
    'gateways' => [
        'errorlog' => [
            'file' => '/tmp/easy-sms.log',
        ],
        'yunpian' => [
            'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
        ],
        'aliyun' => [
            'access_key_id' => '',
            'access_key_secret' => '',
            'sign_name' => '',
        ],
        //...
    ],
];

$easySms = new EasySms($config);

$easySms->send(13188888888, [
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
        'code' => 6379
    ],
]);

$easySms->send(13188888888, [
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
        'code' => 6379
    ],
]);

$easySms->send(13188888888, [
    'content'  => function($gateway){
        return '您的验证码为: 6379';
    },
    'template' => function($gateway){
        return 'SMS_001';
    },
    'data' => function($gateway){
        return [
            'code' => 6379
        ];
    },
]);

$easySms->send(13188888888, [
    'content'  => function($gateway){
        if ($gateway->getName() == 'yunpian') {
            return '云片专用验证码:1235';
        }
        return '您的验证码为: 6379';
    },
    'template' => function($gateway){
        if ($gateway->getName() == 'aliyun') {
            return 'TP2818';
        }
        return 'SMS_001';
    },
    'data' => function($gateway){
        return [
            'code' => 6379
        ];
    },
]);

$easySms->send(13188888888, [
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
        'code' => 6379
    ],
 ], ['yunpian', 'juhe']); // 这里的网关配置将会覆盖全局默认值

[
    'yunpian' => [
        'gateway' => 'yunpian',
        'status' => 'success',
        'result' => [...] // 平台返回值
    ],
    'juhe' => [
        'gateway' => 'juhe',
        'status' => 'failure',
        'exception' => \Overtrue\EasySms\Exceptions\GatewayErrorException 对象
    ],
    //...
]

$e->getResults();               // 返回所有 API 的结果,结构同上
$e->getExceptions();            // 返回所有调用异常列表
$e->getException($gateway);     // 返回指定网关名称的异常对象
$e->getLastException();         // 获取最后一个失败的异常对象

$config = [
    ...
    'default' => [
        'gateways' => [
            'mygateway', // 配置你的网站到可用的网关列表
        ],
    ],
    'gateways' => [
        'mygateway' => [...], // 你网关所需要的参数,如果没有可以不配置
    ],
];

$easySms = new EasySms($config);

// 注册
$easySms->extend('mygateway', function($gatewayConfig){
    // $gatewayConfig 来自配置文件里的 `gateways.mygateway`
    return new MyGateway($gatewayConfig);
});

$easySms->send(13188888888, [
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
        'code' => 6379
    ],
]);

use Overtrue\EasySms\PhoneNumber;

// 发送到国际码为 31 的国际号码
$number = new PhoneNumber(13188888888, 31);

$easySms->send($number, [
    'content'  => '您的验证码为: 6379',
    'template' => 'SMS_001',
    'data' => [
        'code' => 6379
    ],
]);



use Overtrue\EasySms\Message;
use Overtrue\EasySms\Contracts\GatewayInterface;
use Overtrue\EasySms\Strategies\OrderStrategy;

class OrderPaidMessage extends Message
{
    protected $order;
    protected $strategy = OrderStrategy::class;           // 定义本短信的网关使用策略,覆盖全局配置中的 `default.strategy`
    protected $gateways = ['alidayu', 'yunpian', 'juhe']; // 定义本短信的适用平台,覆盖全局配置中的 `default.gateways`

    public function __construct($order)
    {
        $this->order = $order;
    }

    // 定义直接使用内容发送平台的内容
    public function getContent(GatewayInterface $gateway = null)
    {
        return sprintf('您的订单:%s, 已经完成付款', $this->order->no);
    }

    // 定义使用模板发送方式平台所需要的模板 ID
    public function getTemplate(GatewayInterface $gateway = null)
    {
        return 'SMS_003';
    }

    // 模板参数
    public function getData(GatewayInterface $gateway = null)
    {
        return [
            'order_no' => $this->order->no
        ];
    }
}

$order = ...;
$message = new OrderPaidMessage($order);

$easySms->send(13188888888, $message);

    'aliyun' => [
        'access_key_id' => '',
        'access_key_secret' => '',
        'sign_name' => '',
    ],

    'aliyunrest' => [
        'app_key' => '',
        'app_secret_key' => '',
        'sign_name' => '',
    ],

    'aliyunintl' => [
        'access_key_id' => '',
        'access_key_secret' => '',
        'sign_name' => '',
    ],

use Overtrue\EasySms\PhoneNumber;

$easySms = new EasySms($config);
$phone_number = new PhoneNumber(18888888888, 86);

$easySms->send($phone_number, [
    'content' => '您好:先生/女士!您的验证码为${code},有效时间是5分钟,请及时验证。',
    'template' => 'SMS_00000001', // 模板ID
    'data' => [
        "code" => 521410,
    ],
]);

    'yunpian' => [
        'api_key' => '',
        'signature' => '【默认签名】', // 内容中无签名时使用
    ],

    'submail' => [
        'app_id' => '',
        'app_key' => '',
        'project' => '', // 默认 project,可在发送时 data 中指定
    ],

    'luosimao' => [
        'api_key' => '',
    ],

    'yuntongxun' => [
        'app_id' => '',
        'account_sid' => '',
        'account_token' => '',
        'is_sub_account' => false,
    ],

    'huyi' => [
        'api_id' => '',
        'api_key' => '',
        'signature' => '',
    ],

    'juhe' => [
        'app_key' => '',
    ],

    'sendcloud' => [
        'sms_user' => '',
        'sms_key' => '',
        'timestamp' => false, // 是否启用时间戳
    ],

    'baidu' => [
        'ak' => '',
        'sk' => '',
        'invoke_id' => '',
        'domain' => '',
    ],

    'huaxin' => [
        'user_id'  => '',
        'password' => '',
        'account'  => '',
        'ip'       => '',
        'ext_no'   => '',
    ],

    'chuanglan' => [
        'account' => '',
        'password' => '',

        // 国际短信时必填
        'intel_account' => '',
        'intel_password' => '',

        // \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_VALIDATE_CODE  => 验证码通道(默认)
        // \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_PROMOTION_CODE => 会员营销通道
        'channel'  => \Overtrue\EasySms\Gateways\ChuanglanGateway::CHANNEL_VALIDATE_CODE,

        // 会员营销通道 特定参数。创蓝规定:api提交营销短信的时候,需要自己加短信的签名及退订信息
        'sign' => '【通讯云】',
        'unsubscribe' => '回TD退订',
    ],

    'chuanglanv1' => [
        'account' => '',
        'password' => '',
        'needstatus' => false,
        'channel' => \Overtrue\EasySms\Gateways\Chuanglanv1Gateway::CHANNEL_NORMAL_CODE,
    ],

$easySms->send(18888888888, [
    'content' => xxxxxxx
]);

    'chuanglanv1' => [
        'account' => '',
        'password' => '',
        'needstatus' => false,
        'channel' => \Overtrue\EasySms\Gateways\Chuanglanv1Gateway::CHANNEL_VARIABLE_CODE,
    ],

$easySms->send(18888888888, [
    'template' => xxxxxx, // 模板内容
    'data' => 'phone":"15800000000,1234;15300000000,4321',
]);

    'rongcloud' => [
        'app_key' => '',
        'app_secret' => '',
    ]

    'tianyiwuxian' => [
        'username' => '', //用户名
        'password' => '', //密码
        'gwid' => '', //网关ID
    ]

    'twilio' => [
        'account_sid' => '', // sid
        'from' => '', // 发送的号码 可以在控制台购买
        'token' => '', // apitoken
    ],

    'tiniyo' => [
        'account_sid' => '', // auth_id from https://tiniyo.com
        'from' => '', // 发送的号码 可以在控制台购买
        'token' => '', // auth_secret from https://tiniyo.com
    ],

    'qcloud' => [
        'sdk_app_id' => '', // 短信应用的 SDK APP ID
        'secret_id' => '', // SECRET ID
        'secret_key' => '', // SECRET KEY
        'sign_name' => '腾讯CoDesign', // 短信签名
    ],

$easySms->send(18888888888, [
    'template' => 101234, // 模板ID
    'data' => [
        "a", 'b', 'c', 'd', //按占位顺序给值
    ],
]);

    'avatardata' => [
        'app_key' => '', // APP KEY
    ],

    'huawei' => [
        'endpoint' => '', // APP接入地址
        'app_key' => '', // APP KEY
        'app_secret' => '', // APP SECRET
        'from' => [
            'default' => '1069012345', // 默认使用签名通道号
            'custom' => 'csms12345', // 其他签名通道号 可以在 data 中定义 from 来指定
            'abc' => 'csms67890', // 其他签名通道号
            ...
        ],
        'callback' => '' // 短信状态回调地址
    ],

$easySms->send(13188888888, [
    'template' => 'SMS_001',
    'data' => [
        6379
    ],
]);

$easySms->send(13188888888, [
    'template' => 'SMS_001',
    'data' => [
        6379,
        'from' => 'custom' // 对应 config 中的 from 数组中 custom
    ],
]);

    'yunxin' => [
        'app_key' => '',
        'app_secret' => '',
        'code_length' => 4, // 随机验证码长度,范围 4~10,默认为 4
        'need_up' => false, // 是否需要支持短信上行
    ],

$easySms->send(18888888888, [
    'template' => 'SMS_001',    // 不填则使用默认模板
    'data' => [
        'code' => 8946, // 如果设置了该参数,则 code_length 参数无效
        'action' => 'sendCode', // 默认为 `sendCode`,校验短信验证码使用 `verifyCode`
    ],
]);

$easySms->send(18888888888, [
    'template' => 'templateid',    // 模板编号(由客户顾问配置之后告知开发者)
    'data' => [
        'action' => 'sendTemplate', // 默认为 `sendCode`,校验短信验证码使用 `verifyCode`
        'params' => [1,2,3], //短信参数列表,用于依次填充模板
    ],
]);

    'yunzhixun' => [
        'sid' => '',
        'token' => '',
        'app_id' => '',
    ],

$easySms->send(18888888888, [
    'template' => 'SMS_001',
    'data' => [
        'params' => '8946,3',   // 模板参数,多个参数使用 `,` 分割,模板无参数时可为空
        'uid' => 'hexianghui',  // 用户 ID,随状态报告返回,可为空
        'mobiles' => '18888888888,188888888889',    // 批量发送短信,手机号使用 `,` 分割,不使用批量发送请不要设置该参数
    ],
]);

    'kingtto'  => [
        'userid'   => '',
        'account'  => '',
        'password' => '',
    ],

$easySms->send(18888888888, [
    'content'  => '您的验证码为: 6379',
]);

    'qiniu' => [
        'secret_key' => '',
        'access_key' => '',
    ],

$easySms->send(18888888888, [
    'template' => '1231234123412341234',
    'data' => [
        'code' => 1234,
    ],
]);

  'ucloud' => [
        'private_key'  => '',    //私钥
        'public_key'   => '',    //公钥
        'sig_content'  => '',    // 短信签名,
        'project_id'   => '',    //项目ID,子账号才需要该参数
    ],

$easySms->send(18888888888, [
    'template' => 'UTAXXXXX',       //短信模板
    'data' => [
        'code' => 1234,     //模板参数,模板没有参数不用则填写,有多个参数请用数组,[1111,1111]
        'mobiles' =>'',     //同时发送多个手机短信,请用数组[xxx,xxx]
    ],
]);


  'smsbao' => [
        'user'  => '',    //账号
        'password'   => ''   //密码
    ],

$easySms->send(18888888888, [
    'content' => '您的验证码为: 6379',       //短信模板
]);


  'moduyun' => [
        'accesskey' => '',  //必填 ACCESS KEY
        'secretkey' => '',  //必填 SECRET KEY
        'signId'    => '',  //选填 短信签名,如果使用默认签名,该字段可缺省
        'type'      => 0,   //选填 0:普通短信;1:营销短信
    ],

$easySms->send(18888888888, [
    'template' => '5a95****b953',   //短信模板
    'data' => [
        1234,   //模板参数,对应模板的{1}
        30      //模板参数,对应模板的{2}
        //...
    ],
]);


  'rongheyun' => [
        'username' => '',  //必填 用户名
        'password' => '',  //必填 密码
        'signature'=> '',  //必填 已报备的签名
    ],

$easySms->send(18888888888, [
    'template' => '31874',   //短信模板
    'data' => [
        'valid_code' => '888888',   //模板参数,对应模板的{valid_code}
        //...
    ],
]);


  'zzyun' => [
        'user_id' => '',    //必填 会员ID
        'secret' => '',     //必填 接口密钥
        'sign_name'=> '',   //必填 短信签名
    ],

$easySms->send(18888888888, [
    'template' => 'SMS_210317****',   //短信模板
    'data' => [
        'code' => '888888',   //模板参数,对应模板的{code}
        //...
    ],
]);


  'maap' => [
        'cpcode' => '',    //必填 商户编码
        'key' => '',     //必填 接口密钥
        'excode'=> '',   //选填 扩展名
    ],

$easySms->send(18888888888, [
    'template' => '356120',   //短信模板
    'data' => [
        '123465'
    ],//模板参数
]);


    'tinree' => [
        'accesskey' => '', // 平台分配给用户的accesskey
        'secret' => '', // 平台分配给用户的secret
        'sign' => '', // 平台上申请的接口短信签名或者签名ID
    ],

$easySms->send(18888888888, [
    'template' => '123456', // 模板ID
    'data' => [
        "a", 'b', 'c', //按模板变量占位顺序
    ],
]);

  'nowcn' => [
        'key'  => '',    //用户ID
        'secret'   => '',    //开发密钥
        'api_type'  => '',    // 短信通道,
    ],

$easySms->send(18888888888, [
    'content'  => '您的验证码为: 6379',
]);

    'volcengine' => [
        'access_key_id' => '', // 平台分配给用户的access_key_id
        'access_key_secret' => '', // 平台分配给用户的access_key_secret
        'region_id' => 'cn-north-1', // 国内节点 cn-north-1,国外节点 ap-singapore-1,不填或填错,默认使用国内节点
        'sign_name' => '', // 平台上申请的接口短信签名或者签名ID,可不填,发送短信时data中指定
        'sms_account' => '', // 消息组帐号,火山短信页面右上角,短信应用括号中的字符串,可不填,发送短信时data中指定
    ],

$easySms->send(18888888888, [
    'template' => 'SMS_123456', // 模板ID
    'data' => [
       "code" => 1234 // 模板变量
    ],
]);

$easySms->send(18888888888, [
    'template' => 'SMS_123456', // 模板ID
    'data' => [
        "template_param" => ["code" => 1234], // 模板变量参数
        "sign_name" => "yoursignname", // 签名,覆盖配置文件中的sign_name
        "sms_account" => "yoursmsaccount", // 消息组帐号,覆盖配置文件中的sms_account
        "phone_numbers" => "18888888888,18888888889", // 手机号,批量发送,英文的逗号连接多个手机号,覆盖发送方法中的填入的手机号
    ],
]);

    'yidongmasblack' => [
        'ecName' => '', // 机构名称
        'secretKey' => '', // 密钥
        'apId' => '', // 应用ID
        'sign' => '', // 签名
        'addSerial' => '', // 通道号默认空
    ],

$easySms->send(18888888888, [
    'content'  => '您的验证码为: 6379',
]);