阿里云短信官方的java示例是错误的
生活随笔
收集整理的這篇文章主要介紹了
阿里云短信官方的java示例是错误的
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
阿里云短信前身是大魚,現(xiàn)在移入了阿里云短信。
下載了官方sdk發(fā)現(xiàn)一大坨。我不需要啊。
下面自己精簡(jiǎn)一下,寫一個(gè)。
誰(shuí)知道一直提示簽名不正確。
看了官網(wǎng)的示例然后對(duì)比了一下官方的php sdk
這,java示例是錯(cuò)的嘛?
?
這個(gè)是官方的php里的一段,根本沒(méi)有要特殊編碼嘛,只是簡(jiǎn)單urlencode
?
然后自己寫了一個(gè),就不用它的官方php sdk了
<?phpclass 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',
);
//系統(tǒng)參數(shù)放入GET請(qǐng)求串
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;
//發(fā)起HTTP請(qǐng)求
try
{
$resp =$this->fetchContent($url);
}
catch (Exception $e)
{
}
//解析返回結(jié)果
$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文本不是標(biāo)準(zhǔn)JSON或者XML,記下錯(cuò)誤日志
if (false === $respWellFormed)
{
//提示點(diǎn)什么吧
}
//如果返回了錯(cuò)誤碼,記錄到業(yè)務(wù)錯(cuò)誤日志中
if (isset($response->Code)&&$response->Code!='OK')
{
//自己做點(diǎn)什么吧
}
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;
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/etiao/p/9519209.html
總結(jié)
以上是生活随笔為你收集整理的阿里云短信官方的java示例是错误的的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 我的.gitignore下配置。存在这里
- 下一篇: 读书笔记—写给大家看的PPT设计书