PHP 阿里大鱼短信发送,短信查询 (复制可用)

IT-Pony 2016-08-18 PM 478℃ 0条
<?php
class Sms
{
    //换成appkey
    const SMS_APPKEY = '*************';
    //换成自己的secret
    const SMS_SECRET = '*************';
    //短信接口地址
    const URL = 'https://eco.taobao.com/router/rest?';
    /**
     * @param $smsInfo['userCode']     用户id
     * @param $smsInfo['sign_name']    发送短信的签名
     * @param $smsInfo['sms_param']    替换参数 (格式:{"code":111111} 将${code} 替换为111111)
     * @param $smsInfo['mobile']       接受短信的手机号
     * @param $smsInfo['tplcode']      短信模板编号
     */
    public static function Send($smsInfo = null)
    {
        //构造请求参数 (公共参数和必需参数)
        $publicParams = array(
            'method'                => 'alibaba.aliqin.fc.sms.num.send',
            'app_key'               => self::SMS_APPKEY,
            'timestamp'             => date('Y-m-d H:i:s'),
            'format'                => 'json',
            'v'                     => '2.0',
            'sign_method'           => 'md5',
            'extend'                => isset($smsInfo['userCode'])?$smsInfo['userCode']:'',
            'sms_type'              => 'normal',
            'sms_free_sign_name'    => isset($smsInfo['sign_name'])?$smsInfo['sign_name']:'',
            'sms_param'             => isset($smsInfo['sms_param'])?$smsInfo['sms_param']:'',
            'rec_num'               => isset($smsInfo['mobile'])?$smsInfo['mobile']:'',
            'sms_template_code'     => isset($smsInfo['tplcode'])?$smsInfo['tplcode']:''
        );
        //生成签名
        $sign = self::createSign($publicParams);
        //拼装参数
        $strParam = self::createStrParam($publicParams);
        //封装参数
        $strParam .= 'sign='.$sign;
        //获取执行结果
        return file_get_contents(self::URL.$strParam);
    }
    /**
     * @param $smsInfo['mobile'] 查询手机号
     * @param $smsInfo['uptime'] 查询时间
     * @param $smsInfo['page']   当前页
     * @param $smsInfo['size']   一页显示多少条
     */
    public static function Query($smsInfo = null)
    {
        //构造请求参数 (公共参数和必需参数)
        $publicParams = array(
            'method'        => 'alibaba.aliqin.fc.sms.num.query',
            'app_key'       => self::SMS_APPKEY,
            'timestamp'     => date('Y-m-d H:i:s'),
            'format'        => 'json',
            'v'             => '2.0',
            'sign_method'   => 'md5',
            'rec_num'       => isset($smsInfo['mobile'])?$smsInfo['mobile']:'',
            'query_date'    => isset($smsInfo['uptime'])?$smsInfo['uptime']:'',
            'current_page'  => isset($smsInfo['page'])?$smsInfo['page']:1,
            'page_size'     => isset($smsInfo['size'])?$smsInfo['page']:20
        );
        //生成签名
        $sign = self::createSign($publicParams);
        //拼装参数
        $strParam = self::createStrParam($publicParams);
        //封装参数
        $strParam .= 'sign='.$sign;
        //获取执行结果
        return file_get_contents(self::URL.$strParam);
    }

    //签名函数
    protected static function createSign($paramArr)
    {
        $sign = self::SMS_SECRET;
        ksort($paramArr);
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $sign .= $key . $val;
            }
        }
        $sign .= self::SMS_SECRET;
        $sign = strtoupper(md5($sign));
        return $sign;
    }

    //组参函数
    protected static function createStrParam($paramArr)
    {
        $strParam = '';
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $strParam .= $key . '=' . urlencode($val) . '&';
            }
        }
        return $strParam;
    }
}
标签: none

非特殊说明,本博所有文章均为博主原创。

评论啦~