现在的态度决定你未来的高度

阿里云通信新版短信 (复制可用)

<?php

/**
 * 阿里云通信新版短信
 */
class SendSms {

    protected $domain = 'dysmsapi.aliyuncs.com';

    protected $accessKeyId = '**************';

    protected $accessKeySecret = '**************';

    protected $defaultParams = array(
        'RegionId'          => 'cn-hangzhou',
        'Action'            => 'SendSms',
        'Version'           => '2017-05-25',
        'SignName'          => '**************',
    );

    /**
     * 生成签名并发起请求
     *
     * @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
     * @param $accessKeySecret string AccessKeySecret
     * @param $domain string API接口所在域名
     * @param $params array API具体参数
     * @param $security boolean 使用https
     * @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false
     */
    public function request($params, $security=false) {

        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        $apiParams = array_merge(array (
            "SignatureMethod" => "HMAC-SHA1",
            "SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
            "SignatureVersion" => "1.0",
            "AccessKeyId" => $this->accessKeyId,
            "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
            "Format" => "JSON",
        ), array_merge($params, $this->defaultParams));
        ksort($apiParams);

        $sortedQueryStringTmp = '';
        foreach ($apiParams as $key => $value) {
            $sortedQueryStringTmp .= '&' . $this->encode($key) . '=' . $this->encode($value);
        }

        $stringToSign = 'GET&%2F&' . $this->encode(substr($sortedQueryStringTmp, 1));

        $sign = base64_encode(hash_hmac("sha1", $stringToSign, $this->accessKeySecret . '&',true));

        $signature = $this->encode($sign);

        $url = ($security ? 'https' : 'http').'://'.$this->domain.'/?Signature=' . $signature . $sortedQueryStringTmp;

        try {
            $content = $this->fetchContent($url);
            return json_decode($content, true);
        } catch( \Exception $e) {
            return false;
        }
    }

    private function encode($str)
    {
        $res = urlencode($str);
        $res = preg_replace("/\+/", "%20", $res);
        $res = preg_replace("/\*/", "%2A", $res);
        $res = preg_replace("/%7E/", "~", $res);
        return $res;
    }

    private function fetchContent($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "x-sdk-client" => "php/2.0.0"
        ));

        if(substr($url, 0,5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        $rtn = curl_exec($ch);

        if($rtn === false) {
            trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
        }
        curl_close($ch);

        return $rtn;
    }
}

$params = array (
    'PhoneNumbers'      => $mobile,
    'TemplateCode'      => $tplCode,
    'TemplateParam'     => array(),
    'OutId'             => '',
    'SmsUpExtendCode'   => '',
);

$smsCode = mt_rand(11111,999999);

$_SESSION['smsCode'] = $smsCode;

if($smsCode) $params['TemplateParam']['code'] = $smsCode;

$helper = new SendSms();
// 此处可能会抛出异常,注意catch
$content = $helper->request($params);

print_r($content);
发表新评论