云片平台短信发送类

客户需求要用云片短信平台的接口,分分钟写了一个类,发送方便快捷。代码:

<?php

/**
 * 云片短信平台发送短信静态操作类
 * @author WispX
 * @copyright WispX
 * @link http://www.xlogs.cn
 */

class YunPianSms
{
    static $apikey = "这里替换成你的apikey";

    /**
     * 发送短信
     * @param string $mobile 手机号
     * @param string $tplid 短信模板ID
     * @param string $tplval 短信变量替换,格式:'#code# =' . urlencode('1234') . '&' . '#company#' . '=' . urlencode('欢乐行')
     * @return string
     */
    public static function sendSms($mobile, $tplid, $tplval = '') {
        $ch = curl_init();
        $data = array(
            'tpl_id'    => $tplid,
            'tpl_val'   => $tplval,
            'apikey'    => self::$apikey,
            'mobile'    => $mobile
        );
        curl_setopt($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $json_data = curl_exec($ch);
        return json_decode($json_data, true);
    }
}

?>

使用方法:

YunPianSms::sendSms('手机号码', '模板ID', '"#code#=12346"');

1 条评论

  1. Caseysnono

    Show more!..

Leave a Comment