阿里云短信服务 ( PHP类库复制可用 )

IT-Pony 2016-09-23 PM 366℃ 0条

最新版

<?php

class Sms {
    const URL = 'http://sms.market.alicloudapi.com';

    const METHOD = 'GET';

    const URI = '/singleSendSms';

    const APPCODE = '0540660bc62749d9a6dbec9079bdb4aa';

    public static function Send()
    {
        return self::Request([
        'ParamString' => '短信替换值(格式:json字符串)',
        'RecNum' => '手机号',
        'SignName' =>'短信签名', 
        'TemplateCode' => '短信模板编号'
        ]);
    }

    public static function Request($data = [])
    {
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . self::APPCODE);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, self::METHOD);
        curl_setopt($curl, CURLOPT_URL, self::URL.self::URI.'?'.http_build_query($data));
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos(self::URL, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result = curl_exec($curl);
        curl_close($curl);

        return $result;
    }
}

print_r(Sms::Send());

旧版本

<?php 
  
class Sms {
    //用户的appkey
    const SMS_APPKEY     = '**********';
    //用户的appsecret
    const SMS_SECRET     = '**********';
    //请求方式
    const METHOD         = 'GET';
    //请求地址
    const URL             = 'http://sms.market.alicloudapi.com';
    //请求路由
    const URI             = '/singleSendSms';
    //以什么格式请求
    const ACCEPT         = 'application/json;charset=utf-8';

    //发送短信
    public static function Send($smsParams)
    {
        $request_paras = array(
            'ParamString' => '短信替换值(格式:json字符串)',
            'RecNum' => '手机号',
            'SignName' =>'短信签名', 
            'TemplateCode' => '短信模板编号'
        );
            //组装参数
        $params = self::createStrParam($request_paras);
            //创建参数
        $signParmas = self::createSign($params);
            //请求
        $result = self::request($signParmas);

    }

     //签名函数
    protected static function createSign($paramArr)
    {
        $content_md5 = $date = $sign_str = "";
        $sign_str .= self::METHOD."\n";
        $sign_str .= self::ACCEPT."\n";
        $sign_str .= $content_md5."\n";
        $sign_str .= "\n";
        $sign_str .= $date."\n";
        $sign_str .= $paramArr['header_str'];
        $sign_str .= $paramArr['url_str'];

        $sign = base64_encode(hash_hmac('sha256', $sign_str, self::SMS_SECRET, true));
        $paramArr['headers']['X-Ca-Signature'] = $sign;
        $paramArr['headers']['X-Ca-Signature-Headers'] = join(',', $paramArr['sig_header']);
        
        return array('headers' => $paramArr['headers'],'url_str' => $paramArr['url_str']);
    }
    //组合参数
    protected static function createStrParam($request_params)
    {
        $para_array = $sig_header = array();

        $content_type = $header_str =  "";

        ksort($request_params);
       
        $headers = array(
            'X-Ca-Key' => self::SMS_APPKEY,
            'Accept' => self::ACCEPT
        );
        ksort($headers);

        $header_ignore_list = array('X-CA-SIGNATURE', 'X-CA-SIGNATURE-HEADERS', 'ACCEPT', 'CONTENT-MD5', 'CONTENT-TYPE', 'DATE');
        
        foreach($headers as $k => $v) {
            if(in_array(strtoupper($k), $header_ignore_list)) {
                continue;
            }
            $header_str .= $k . ':' . $v . "\n";
            array_push($sig_header, $k);
        }
        
        $url_str = self::URI;
        
        foreach($request_params as $k => $v) {
            array_push($para_array, $k .'='. $v);
        }
        if(!empty($para_array)) {
            $url_str .= '?' . join('&', $para_array);
        }

        return array('header_str' => $header_str,'url_str' => $url_str,'sig_header' => $sig_header,'headers' => $headers);
    }
    //请求短信接口
    protected static function request($request)
    {
        $request_header = array();

        foreach($request['headers'] as $k => $v) {
            array_push($request_header, $k .': ' . $v);
        }

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, self::URL . $request['url_str']);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT,true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_header);
        curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        return $info;
    }
}
    //调用可使用
Sms::Send();

使用说明

将需要的参数或者信息填入,需要稍微修改一下,就可用!

标签: none

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

评论啦~