阿里云短信官方的java示例是错误的

阿里云短信前身是大鱼,现在移入了阿里云短信。

下载了官方sdk发现一大坨。我不需要啊。

下面自己精简一下,写一个。

谁知道一直提示签名不正确。

看了官网的示例然后对比了一下官方的php sdk

这,java示例是错的嘛?

 

这个是官方的php里的一段,根本没有要特殊编码嘛,只是简单urlencode

 

然后自己写了一个,就不用它的官方php sdk了

<?php

class SmsClient
{

public $appKey;
public $secretKey;
public $gatewayUrl = "http://dysmsapi.aliyuncs.com";
public $format = "xml";



public function __construct($appKey = "",$secretKey = ""){
$this->appKey = $appKey;
$this->secretKey = $secretKey ;
}



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

private function computeSignature($parameters, $accessKeySecret) {
$signString = '';
foreach($parameters as $key => $value)
{
$signString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
}
$stringToSign = 'GET&%2F&' . $this->percentEncode(substr($signString, 1));
$signature=base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret."&", true));
return $signature;
}
public function execute($request, $bestUrl = null)
{
header("Content-type: text/html; charset=utf-8");

$sysParams = array(
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce' => $this->create_uuid(),
'AccessKeyId' => $this -> appKey,
'SignatureVersion' => '1.0',
'Timestamp' =>gmdate("Y-m-d\TH:i:s\Z"),
'Format' =>$this->format,
'Action'=> 'SendSms',
'Version'=>'2017-05-25',
'RegionId'=>'cn-hangzhou',
'PhoneNumbers' => $request->getRecNum(),
'SignName' => $request->getSmsFreeSignName(),
'TemplateParam' =>$request->getSmsParam(),
'TemplateCode' => $request->getSmsTemplateCode(),
'OutId'=>'12345',
);

//系统参数放入GET请求串
if($bestUrl){
$requestUrl = $bestUrl."/?";
}else{
$requestUrl = $this->gatewayUrl."/?";
}
ksort($sysParams);
$sortedQueryString= "";
foreach ($sysParams as $key => $value) {
$sortedQueryString.= "&".$key."=" . urlencode($value);
}
//签名
$signature = $this->computeSignature($sysParams,$this->secretKey);
$url=$requestUrl.'Signature='.$signature.$sortedQueryString;

//发起HTTP请求
try
{
$resp =$this->fetchContent($url);

}
catch (Exception $e)
{


}

//解析返回结果
$response=null;
$respWellFormed = false;
if ("JSON" == $this->format)
{
$response = json_decode($resp);

if (null !== $response)
{
$respWellFormed = true;
}
}
else if("XML" == $this->format)
{
$response = @simplexml_load_string($resp);
if (false !== $response)
{
$respWellFormed = true;
}
}

//返回的HTTP文本不是标准JSON或者XML,记下错误日志
if (false === $respWellFormed)
{
//提示点什么吧
}

//如果返回了错误码,记录到业务错误日志中
if (isset($response->Code)&&$response->Code!='OK')
{
//自己做点什么吧
}
return $response;
}


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;
}
function create_uuid($prefix = ""){ //可以指定前缀
$str = md5(uniqid(mt_rand(), true));
$uuid = substr($str,0,8) . '-';
$uuid .= substr($str,8,4) . '-';
$uuid .= substr($str,12,4) . '-';
$uuid .= substr($str,16,4) . '-';
$uuid .= substr($str,20,12);
return $prefix . $uuid;
}
}
posted @ 2018-08-22 17:38  伊条  阅读(904)  评论(0编辑  收藏  举报