使用阿里云短信api,需要在控制台获取以下必要参数,其中需要自己手机验证+官方审核多次,尤其审核需要保持耐心。

1. accesskeyid  相当于你的个人账户密钥;

2. accesskeysecret 与上是成对的;

3. signname  个人签名,在发出去的短信中,这个签名会显示在开头,类似 【签名】亲爱的用户…… 这种格式,signname需要通过提交审核;

4.templatecode  模板代码,阿里云短信是无法完全自定义短信的,需要通过审核的模板,然后自己再替换掉模板中的变量,如模板:“您的验证码是${code}” ,code就是变量,使用时需设置变量值{“code”:”12345”}(设置变量值的过程在demo中实现),短信发出去后变成:“您的验证码是12345”,每个通过审核的模板会提供一个模板代码;

最新的阿里云短信接口,适用于阿里大于搬家以后的情况。
之前一直用阿里大于的短信接口,最近上项目时发现阿里大于悄悄地搬家到了阿里云!阿里云的sdk文件繁多,看得一头雾水!下面代码是最新的可适用于阿里云短信服务的类,亲测成功!

<?php

/**
 * 阿里云短信验证码发送类
 * @author administrator
 *
 */

class sms {

    // 保存错误信息

    public $error;

    // access key id

    private $accesskeyid = '';

    // access access key secret

    private $accesskeysecret = '';

    // 签名

    private $signname = '';

    // 模版id

    private $templatecode = '';

    public function __construct($cofig = array()) {

        $cofig = array (

                'accesskeyid' => 'xxxxxxxxxxx',

                'accesskeysecret' => 'xxxxxxxxxx',

                'signname' => '你的签名',

                'templatecode' => 'sms_76510109'

        );

        // 配置参数

        $this->accesskeyid = $cofig ['accesskeyid'];

        $this->accesskeysecret = $cofig ['accesskeysecret'];

        $this->signname = $cofig ['signname'];

        $this->templatecode = $cofig ['templatecode'];

    }

    private function percentencode($string) {

        $string = urlencode ( $string );

        $string = preg_replace ( '/\+/', '%20', $string );

        $string = preg_replace ( '/\*/', '%2a', $string );

        $string = preg_replace ( '/%7e/', '~', $string );

        return $string;

    }

    /**
     * 签名
     *
     * @param unknown $parameters            
     * @param unknown $accesskeysecret            
     * @return string
     */

    private function computesignature($parameters, $accesskeysecret) {

        ksort ( $parameters );

        $canonicalizedquerystring = '';

        foreach ( $parameters as $key => $value ) {

            $canonicalizedquerystring .= '&' . $this->percentencode ( $key ) . '=' . $this->percentencode ( $value );

        }

        $stringtosign = 'get&%2f&' . $this->percentencode ( substr ( $canonicalizedquerystring, 1 ) );

        $signature = base64_encode ( hash_hmac ( 'sha1', $stringtosign, $accesskeysecret . '&', true ) );

        return $signature;

    }

    /**
     * @param unknown $mobile            
     * @param unknown $verify_code            
     *
     */

    public function send_verify($mobile, $verify_code) {

        $params = array (   //此处作了修改

                'signname' => $this->signname,

                'format' => 'json',

                'version' => '2017-05-25',

                'accesskeyid' => $this->accesskeyid,

                'signatureversion' => '1.0',

                'signaturemethod' => 'hmac-sha1',

                'signaturenonce' => uniqid (),

                'timestamp' => gmdate ( 'y-m-d\th:i:s\z' ),

                'action' => 'sendsms',

                'templatecode' => $this->templatecode,

                'phonenumbers' => $mobile,

                //'templateparam' => '{"code":"' . $verify_code . '"}' 

                'templateparam' => '{"time":"1234"}'   //更换为自己的实际模版

        );

        //var_dump($params);die;

        // 计算签名并把签名结果加入请求参数

        $params ['signature'] = $this->computesignature ( $params, $this->accesskeysecret );

        // 发送请求(此处作了修改)

        //$url = 'https://sms.aliyuncs.com/?' . http_build_query ( $params );

        $url = 'http://dysmsapi.aliyuncs.com/?' . http_build_query ( $params );



        $ch = curl_init ();

        curl_setopt ( $ch, curlopt_url, $url );

        curl_setopt ( $ch, curlopt_ssl_verifypeer, false );

        curl_setopt ( $ch, curlopt_ssl_verifyhost, false );

        curl_setopt ( $ch, curlopt_returntransfer, 1 );

        curl_setopt ( $ch, curlopt_timeout, 10 );

        $result = curl_exec ( $ch );

        curl_close ( $ch );

        $result = json_decode ( $result, true );

        //var_dump($result);die;

        if (isset ( $result ['code'] )) {

            $this->error = $this->geterrormessage ( $result ['code'] );

            return false;

        }

        return true;

    }

    /**
     * 获取详细错误信息
     *
     * @param unknown $status            
     */

    public function geterrormessage($status) {

        // 阿里云的短信 乱八七糟的(其实是用的阿里大于)

        // https://api.alidayu.com/doc2/apidetail?spm=a3142.7629140.1.19.smdyoa&apiid=25450

        $message = array (

                'invaliddayustatus.malformed' => '账户短信开通状态不正确',

                'invalidsignname.malformed' => '短信签名不正确或签名状态不正确',

                'invalidtemplatecode.malformed' => '短信模板code不正确或者模板状态不正确',

                'invalidrecnum.malformed' => '目标手机号不正确,单次发送数量不能超过100',

                'invalidparamstring.malformed' => '短信模板中变量不是json格式',

                'invalidparamstringtemplate.malformed' => '短信模板中变量与模板内容不匹配',

                'invalidsendsms' => '触发业务流控',

                'invaliddayu.malformed' => '变量不能是url,可以将变量固化在模板中'

        );

        if (isset ( $message [$status] )) {

            return $message [$status];

        }

        return $status;

    }

}

调用方法:

//生成验证码
$mobile = 'xxxxxxx';
$code = rand ( 1000, 9999 );
//发送短信
$sms = new sms();

//测试模式
$status = $sms->send_verify($mobile, $code);
if (!$status) {
  echo $sms->error;

}

 

php与九大接口实战视频教程【80节】

链接:http://www.mano100.cn/thread-180-1-1.html