摘要:新版下載下來(lái),集成了很多東西,自己看著都煩,不多說(shuō),上源碼我寫(xiě)了兩個(gè)類(lèi)線上地址公共參數(shù)發(fā)送短信電話號(hào)碼短信簽名短信模板代碼短信模板參數(shù)缺少參數(shù)缺少參數(shù)缺少參數(shù)缺少參數(shù)計(jì)算簽名網(wǎng)絡(luò)請(qǐng)求
新版sdk下載下來(lái),集成了很多東西,自己看著都煩,不多說(shuō),上源碼
我寫(xiě)了兩個(gè)類(lèi)
AliSms.class.php
class AliSms {
//線上地址 const API_DOAMIN = "http://dysmsapi.aliyuncs.com/"; protected $env; protected $accesskey; protected $secretKey; protected static $method = "POST"; protected static $header = array( "x-sdk-client" => "php/2.0.0", ); //30 second public static $connectTimeout = 30; //80 second public static $readTimeout = 80; //公共參數(shù) protected $requestParams = array();
public function __construct($accessKey,$secretKey){ $this->accesskey = $accessKey; $this->secretKey = $secretKey; $this->requestParams["AccessKeyId"] = $this->accesskey; $this->requestParams["RegionId"] = "cn-hangzhou"; $this->requestParams["Format"] = "JSON"; $this->requestParams["SignatureMethod"] = "HMAC-SHA1"; $this->requestParams["SignatureVersion"] = "1.0"; $this->requestParams["SignatureNonce"] = uniqid(); date_default_timezone_set("GMT"); $this->requestParams["Timestamp"] = date("Y-m-dTH:i:s"); $this->requestParams["Action"] = "SendSms"; $this->requestParams["Version"] = "2017-05-25"; } /** * 發(fā)送短信 * @param $phoneNumbers 電話號(hào)碼 * @param $signName 短信簽名 * @param $templateCode 短信模板代碼 * @param $tempalteParam 短信模板參數(shù) * @return HttpResponse * @throws ThinkException */ public function execute($phoneNumbers, $signName, $templateCode, $tempalteParam){ $params = array( "PhoneNumbers" => $phoneNumbers, "SignName" => $signName, "TemplateCode" => $templateCode, "TemplateParam" => $tempalteParam, ); if(empty($params["PhoneNumbers"])) { throw new Exception("缺少參數(shù)PhoneNumbers"); } if(empty($params["SignName"])) { throw new Exception("缺少參數(shù)SignName"); } if(empty($params["TemplateCode"])) { throw new Exception("缺少參數(shù)TemplateCode"); } if(empty($params["TemplateParam"])) { throw new Exception("缺少參數(shù)TemplateParam"); } foreach ($params as $key => $value) { $apiParams[$key] = $this->prepareValue($value); } $params = array_merge($params, $this->requestParams); $params["Signature"] = $this->computeSignature($params, $this->secretKey); $response = $this->curl(self::API_DOAMIN, self::$method, $params, self::$header); return $response; } //計(jì)算簽名 public function computeSignature($parameters, $accessKeySecret) { ksort($parameters); $canonicalizedQueryString = ""; foreach($parameters as $key => $value) { $canonicalizedQueryString .= "&" . $this->percentEncode($key). "=" . $this->percentEncode($value); } $stringToSign = self::$method."&%2F&" . $this->percentencode(substr($canonicalizedQueryString, 1)); $signature = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret."&", true)); return $signature; }
private function prepareValue($value) { if (is_bool($value)) { if ($value) { return "true"; } else { return "false"; } } else { return $value; } }
public function percentEncode($str) { $res = urlencode($str); $res = preg_replace("/+/", "%20", $res); $res = preg_replace("/*/", "%2A", $res); $res = preg_replace("/%7E/", "~", $res); return $res; } /** * 網(wǎng)絡(luò)請(qǐng)求 * @param $url * @param string $httpMethod * @param null $postFields * @param null $headers * @return mixed * @throws ThinkException */ public function curl($url, $httpMethod = "GET", $postFields = null,$headers = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? self::getPostHttpBody($postFields) : $postFields); if (self::$readTimeout) { curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout); } if (self::$connectTimeout) { curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout); } //https request if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } if (is_array($headers) && 0 < count($headers)) { $httpHeaders =self::getHttpHearders($headers); curl_setopt($ch,CURLOPT_HTTPHEADER,$httpHeaders); } $httpResponse = curl_exec($ch); if (curl_errno($ch)) { throw new Exception("Server unreachable: Errno: " . curl_errno($ch) . curl_error($ch)); } curl_close($ch); return $httpResponse; } public static function getPostHttpBody($postFildes){ $content = ""; foreach ($postFildes as $apiParamKey => $apiParamValue) { $content .= "$apiParamKey=" . urlencode($apiParamValue) . "&"; } return substr($content, 0, -1); } public static function getHttpHearders($headers) { $httpHeader = array(); foreach ($headers as $key => $value) { array_push($httpHeader, $key.":".$value); } return $httpHeader; }
}
下面這個(gè)類(lèi)只是我不想調(diào)用是new AliSms寫(xiě)的
Sender.class.php
class Sender {
private $provider; private static $_instance = null; private function __construct() { $this->provider = new AliSms(C("ALIYUN_AK"),C("ALIYUN_SK")); } //單例 public static function getInstance() { if(!self::$_instance) { self::$_instance = new Sender(); } return self::$_instance; } /** * 發(fā)送短信驗(yàn)證碼 * @param $mobile 電話號(hào)碼 * @param $data 驗(yàn)證碼 array("code"=>"123456") * @return bool */ public function sendCode($mobile,$data) { if(!$data["code"]) return false; $tempalteParam = json_encode($data); $result = $this->provider->execute($mobile,"短信簽名","短信模板編號(hào)",$tempalteParam); return $this->parseResult($result,$mobile); }
/** * 處理短信發(fā)送返回結(jié)果 * @param $result * @param string $mobile * @return bool */ private function parseResult($result,$mobile = "") { $result = json_decode($result,1); if($result["Code"] == "OK" && $result["Message"] == "OK"){ //發(fā)送成功 $this->onSendSuccess($result,$mobile); return true; }else{ //失敗 $this->onSendFail($result,$mobile); return false; } } /** * 發(fā)送成功后 * @param array $data * @param string $mobile */ private function onSendSuccess(array $data = null ,$mobile = "") { //todo } /** * 發(fā)送失敗后 * @param array $data * @param string $mobile */ private function onSendFail(array $data = null , $mobile = "") { //記錄日志 $logPath = RUNTIME_PATH."sms_".date("y_m_d").".log"; ThinkLog::write( " 手機(jī)號(hào):".$mobile. " 大魚(yú)返回結(jié)果:".serialize($data), "INFO", "", $logPath ); }
使用方法:
$result = Sender::getInstance()->sendCode($mobile,array("code" => $randCode));
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/26011.html
摘要:阿里大魚(yú)兼容服務(wù)器端環(huán)境,模塊加載器如和所有瀏覽器瀏覽器直接調(diào)用需要兩個(gè)的依賴(lài)文件和,其中網(wǎng)上版本很多,這里使用腳本標(biāo)簽引入文件如示例腳本代碼應(yīng)用密匙見(jiàn)創(chuàng)建實(shí)例參數(shù)見(jiàn)身份驗(yàn)證短信發(fā)送端注意,信息可能泄露出來(lái),可以使用混淆加密代碼。 JavaScript Alidayu(阿里大魚(yú)) SDK 兼容服務(wù)器端環(huán)境node.js,模塊加載器如RequireJS和所有瀏覽器 Client-sid...
摘要:經(jīng)過(guò)各大短信平臺(tái)進(jìn)行比較后,選擇了阿里大于,一個(gè)阿里巴巴的云通信平臺(tái),下面我將這次開(kāi)發(fā)經(jīng)驗(yàn)和遇到的一些問(wèn)題分享出來(lái)。 最近在做一個(gè)商城的項(xiàng)目,其中注冊(cè)、找回密碼、換綁手機(jī)等功能都需要用到驗(yàn)證碼,考慮到上線的安全問(wèn)題,我決定用手機(jī)驗(yàn)證碼來(lái)提高安全性。經(jīng)過(guò)各大短信平臺(tái)進(jìn)行比較后,選擇了阿里大于,一個(gè)阿里巴巴的云通信平臺(tái),下面我將這次開(kāi)發(fā)經(jīng)驗(yàn)和遇到的一些問(wèn)題分享出來(lái)。 1.登錄平臺(tái) 阿里大...
摘要:經(jīng)過(guò)各大短信平臺(tái)進(jìn)行比較后,選擇了阿里大于,一個(gè)阿里巴巴的云通信平臺(tái),下面我將這次開(kāi)發(fā)經(jīng)驗(yàn)和遇到的一些問(wèn)題分享出來(lái)。 最近在做一個(gè)商城的項(xiàng)目,其中注冊(cè)、找回密碼、換綁手機(jī)等功能都需要用到驗(yàn)證碼,考慮到上線的安全問(wèn)題,我決定用手機(jī)驗(yàn)證碼來(lái)提高安全性。經(jīng)過(guò)各大短信平臺(tái)進(jìn)行比較后,選擇了阿里大于,一個(gè)阿里巴巴的云通信平臺(tái),下面我將這次開(kāi)發(fā)經(jīng)驗(yàn)和遇到的一些問(wèn)題分享出來(lái)。 1.登錄平臺(tái) 阿里大...
摘要:說(shuō)明阿里大魚(yú)提供了驗(yàn)證碼,短信通知,語(yǔ)音等服務(wù),在使用后感覺(jué)挺方便,不愧是阿里旗下的產(chǎn)品。最近想搞個(gè)發(fā)送短信通知的功能,不過(guò)阿里大魚(yú)官網(wǎng)并沒(méi)有提供版本的示例沒(méi)有版本的,所以需要自己整一個(gè)簽名,實(shí)現(xiàn)短信發(fā)送。 1、說(shuō)明 阿里大魚(yú)提供了驗(yàn)證碼,短信通知,語(yǔ)音等服務(wù),在使用后感覺(jué)挺方便,不愧是阿里旗下的產(chǎn)品。 最近想搞個(gè)NodeJS發(fā)送短信通知的功能,不過(guò)阿里大魚(yú)官網(wǎng)API并沒(méi)有提供JS版本...
摘要:之前使用的阿里大于,不過(guò)很坑的是,新接入的都不能用了,融入進(jìn)了阿里云服務(wù),當(dāng)然阿里大于的老用戶還可以繼續(xù)用阿里大于首先還是接入,上圖找到短信服務(wù)設(shè)置短信簽名和短信模板設(shè)置或找到或下載文檔打開(kāi)下載下來(lái)的文檔,只需要,將其改名為,并放到項(xiàng)目根目 之前使用的阿里大于,不過(guò)很坑的是,新接入的都不能用了,融入進(jìn)了阿里云服務(wù),當(dāng)然阿里大于的老用戶還可以繼續(xù)用阿里大于 首先還是接入,上圖: (1)找...
閱讀 2750·2023-04-25 14:15
閱讀 2698·2021-11-04 16:11
閱讀 3393·2021-10-14 09:42
閱讀 438·2019-08-30 15:52
閱讀 2824·2019-08-30 14:03
閱讀 3544·2019-08-30 13:00
閱讀 2110·2019-08-26 11:40
閱讀 3306·2019-08-26 10:25