您好,欢迎来到码863代码分享网! 请[登录] / [注册]

精简阿里云短信服务官方SDK及DEMO

编辑

原文 https://blog.csdn.net/ygc123189/article/details/75103251


github https://github.com/qqtxt/aliyun_sms 亲测可用 2019-9-21


官方SDK及DEMO,包含众多的文件夹及文件,一大堆的类,类引用地址也是乱的,没法直接运行。


    里面包含多个接口(短信发送API,短信查询API,短信消息API),项目只需要短信发送接口即可。


    精简后的代码如下:(精简采用POST方式JSON格式SHA1加密)

        $url = 'http://dysmsapi.aliyuncs.com/';
		$accessKeyId = "yourAccessKeyId";
		$accessKeySecret = "yourAccessKeySecret";
		date_default_timezone_set("GMT");
		$Timestamp = date('Y-m-d\TH:i:s\Z');
 
		$parameters = [
			'PhoneNumbers' => '15000000000',
			'SignName' => '短信签名',
			'TemplateCode' => '短信模板Code',
			'TemplateParam' => '{"code":"123456"}',
			'OutId' => '1234',
			'RegionId' => 'cn-hangzhou',
			'AccessKeyId' => $accessKeyId,
			'Format' => 'JSON',
			'SignatureMethod' => 'HMAC-SHA1',
			'SignatureVersion' => '1.0',
			'SignatureNonce' => uniqid(),
			'Timestamp' => $Timestamp,
			'Action' => 'SendSms',
			'Version' => '2017-05-25',
		];
		$Signature = $this->computeSignature($parameters, $accessKeySecret);
		$parameters['Signature'] = $Signature;
		echo curl_post($url,$parameters);
        public function computeSignature($parameters, $accessKeySecret)
	{
	    ksort($parameters);
	    $canonicalizedQueryString = '';
	    foreach($parameters as $key => $value)
	    {
			$canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
	    }	
	    $stringToSign = 'POST'.'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
		$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret."&", true));
	    return $signature;
	}
	
	protected function percentEncode($str)
	{
	    $res = urlencode($str);
	    $res = preg_replace('/\+/', '%20', $res);
	    $res = preg_replace('/\*/', '%2A', $res);
	    $res = preg_replace('/%7E/', '~', $res);
	    return $res;
        }


CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网 湘ICP备2023018646号-1
MYSQl共执行 3 个查询,用时 0.0018680095672607 秒,PHP脚本用时 0.003804 秒,占用内存 0.508 MB,Gzip 已启用