Ali cloud SMS service code templates

<?php
 
namespace app\api\controller;
use think\Db;
use think\Request;
use think\Controller;
/**
 * Ali cloud send SMS verification code class
 * @author Administrator
 *
 */
 
class Smscode {
 
    // save the error messages
 
    public $error;
 
    // Access Key ID
 
    private $accessKeyId = '';
 
    // Access Access Key Secret
 
    private $accessKeySecret = '';
 
    // Signature
 
    private $signName = '';
 
    // ID Templates
 
    private $templateCode = '';
 
    public function __construct($cofig = array()) {
 
        $cofig = array (
 
                'accessKeyId' => 'XXXXXXXXXXXXXXXXXXXX',
 
                'accessKeySecret' => 'XXXXXXXXXXXXXXXXXXXX',
 
                'signName' => 'XXXX',
 
                'templateCode' => 'SMS_XXXXXXX'
 
        );
 
        // configuration parameters
 
        $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;
 
    }
 
    /**
     * Signed
     *
     * @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, $code) {
 
        $ Params = array (// here was modified
 
                '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":"' . $code . '"}'
 
        );
 
        //var_dump($params);die;
 
        // Calculate the signature and the signature added to the results request parameters
 
        $params ['Signature'] = $this->computeSignature ( $params, $this->accessKeySecret );
 
        // send the request (here was modified)
 
        //$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;
 
    }
 
    /**
     * Get detailed error information
     *
     * @param unknown $status           
     */
 
    public function getErrorMessage($status) {
 
        // Ali cloud messaging chaos eighty-seven bad (in fact, is greater than Ali)
 
        // https://api.alidayu.com/doc2/apiDetail?spm=a3142.7629140.1.19.SmdYoA&apiId=25450
 
        $message = array (
 
                'InvalidDayuStatus.Malformed' => 'message accounts open state incorrect',
 
                'InvalidSignName.Malformed' => 'message incorrect signature or signature status is not correct,'
 
                'InvalidTemplateCode.MalFormed' => 'Code message template or template incorrect state incorrect',
 
                'InvalidRecNum.Malformed' => 'target phone number is incorrect, a single transmission can not exceed the number 100',
 
                'InvalidParamString.MalFormed' => 'message template variables are not json format'
 
                'InvalidParamStringTemplate.Malformed' => 'message template variable contents do not match the template',
 
                'InvalidSendSms' => 'traffic flow control trigger',
 
                'InvalidDayu.Malformed' => 'variables can not url, variables can be cured in the template'
 
        );
 
        if (isset ( $message [$status] )) {
 
            return $message [$status];
 
        }
 
        return $status;
 
    }
 
}
 
// external calls
$phone = $_POST['phone'];
if(empty($phone))
{
   echo '{ "status": 0, "message": "missing argument"}';
   exit;
}
SMScode SMScode $ = new ();
$code = rand(111111,999999);
$this->send_verify($phone,$code);

Guess you like

Origin www.cnblogs.com/lzp4510/p/12311203.html