爱吃醋的饺子's BLOG

浏览数(8680)

【大于短信极简版】

阿里的大于短信接口太繁琐,文件一堆,最新版本合并到阿里云了,无比扯淡。让我想起了被人安利设计模式的恶心回忆。

基本逻辑:获取参数->按要求组装->发起请求->获取结果

https://help.aliyun.com/document_detail/55359.html?spm=5176.8195934.507901.12.b1ngGK

/**请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign

* 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template

* @Author Dumplings

* @DateTime 2021-08-19T22:02:29+0800

* @Email [i@xiv.cm]

* @Version [1.0]

* @return [type]

*/

class Dysms{

public static $connectTimeout = 30;//30 second

public static $readTimeout = 80;//80 second

private $Method='GET';

private $Content;

private $conf=[

'AccessKeyId' => 'xxxxxxxxxxx',// TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)

'accessKeySecret' => 'xxxxxxxxxx',// TODO 此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)

'domain'=>'dysmsapi.aliyuncs.com',//产品域名,开发者无需替换

'SignName' => '你的签名',// 必填,设置签名名称,应严格按"签名名称"填写,

'templateCode' => 'SMS_76510109',// 必填,设置模板CODE,应严格按"模板CODE"填写,

'RegionId'=>'cn-hangzhou',// 服务结点

'Format'=>'JSON',//请求格式

'SignatureMethod'=>'HMAC-SHA1',//加密方式

'SignatureVersion'=>'1.0',//版本

'Action'=>'SendSms',//SendBatchSms//QuerySendDetails

'Version'=>'2017-05-25',//接口版本

'protocol'=>'http',//接口协议

'Method'=>'GET',//请求方式

'product'=>'Dysmsapi'//产品名称

];

public function __construct($config = array()) {

//$this->conf=$config;

foreach($config as $key =>$value){

if(!empty($value)){

$this->conf[$key]=$value;

}

}

}

static function getPostHttpBody($postFildes){

$content = "";

foreach ($postFildes as $apiParamKey => $apiParamValue)

{

$content .= "$apiParamKey=" . urlencode($apiParamValue) . "&";

}

return substr($content, 0, -1);

}

static function getHttpHearders($headers){

$httpHeader = array();

foreach ($headers as $key => $value)

{

array_push($httpHeader, $key.":".$value);

}

return $httpHeader;

}

public static function curl($url, $httpMethod = "GET", $postFields = null,$headers = null) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);

/*if(ENABLE_HTTP_PROXY) {

curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);

curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP);

curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);

}*/

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 =[];

$httpResponse['body']=curl_exec($ch);

$httpResponse['Status']=curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch))

{

exit("Server unreachable: Errno: " . curl_errno($ch) . " " . curl_error($ch). "SDK.ServerUnreachable");

}

curl_close($ch);

return $httpResponse;

}

public function prepareValue($value) {

if (is_bool($value)) {

if ($value) {

return "true";

} else {

return "false";

}

} else {

return $value;

}

}

public function computeSignature($parameters, $accessKeySecret){

ksort($parameters);

$canonicalizedQueryString = '';

foreach($parameters as $key => $value)

{

$canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);

}

$stringToSign = $this->conf['Method'].'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));

$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret."&", true));

return $signature;

}

public function percentEncode($str){

$res = urlencode($str);

$res = preg_replace('/\+/', '%20', $res);

$res = preg_replace('/\*/', '%2A', $res);

$res = preg_replace('/%7E/', '~', $res);

return $res;

}

public $domainParameters = [];

function sendsms($sms){

foreach ($sms as $key => $value) {

$sms[$key] = $this->prepareValue($value);

}

$sms["RegionId"] = $this->conf['RegionId'];

$sms["AccessKeyId"] =$this->conf['AccessKeyId'];

$sms["Format"] = $this->conf['Format'];

$sms["SignatureMethod"] =$this->conf['SignatureMethod'];

$sms["SignatureVersion"] = $this->conf['SignatureVersion'];

$sms["SignatureNonce"] = md5(uniqid(mt_rand(), true));

$sms["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');

$sms["Action"] = $this->conf['Action'];//SendBatchSms//QuerySendDetails

$sms["Version"] = $this->conf['Version'];

$sms["Signature"] =self::computeSignature($sms, $this->conf['accessKeySecret']);

if($this->conf['Method'] == "POST") {

$requestUrl = $this->conf['protocol']."://". $this-conf['domain'] . "/";

foreach ($sms as $apiParamKey => $apiParamValue)

{

$this->domainParameters[$apiParamKey]=$apiParamValue;

}

$httpResponse = self::curl($requestUrl,$this->conf['Method'], $this->getDomainParameter(), $headers);

}

else {

$requestUrl = $this->conf['protocol']."://". $this->conf['domain'] . "/?";

foreach ($sms as $apiParamKey => $apiParamValue)

{

$requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&";

}

$requestUrl= substr($requestUrl, 0, -1);

$httpResponse = self::curl($requestUrl, $this->conf['Method'], $this->Content, $headers);

}

$retryTimes = 1;

while (500 <= $httpResponse['Status'] && $autoRetry && $retryTimes < $maxRetryNumber) {

$requestUrl = $request->composeUrl($iSigner, $credential,$domain);

if(count($request->getDomainParameter())>0){

$httpResponse = self::curl($requestUrl, $this->conf['Method'], $request->getDomainParameter(), $request->getHeaders());

} else {

$httpResponse = self::curl($requestUrl, $this->conf['Method'], $this->Content, $request->getHeaders());

}

$retryTimes ++;

}

return $httpResponse;

}

}

$sms=new Dysms(['AccessKeyId' => 'xxxxxxxxxxx',

'accessKeySecret' => 'xxxxxxxxxx',

'SignName' => '你的签名',

'templateCode' => 'SMS_76510109',

'protocol'=>'http',

'Method'=>'GET']);

$re=$sms->sendsms(['PhoneNumbers'=>'qqqq','TemplateParam'=>json_encode(array("code"=>"1234"), JSON_UNESCAPED_UNICODE)]);

print_r($re);

✎﹏₯㎕﹍﹍·狗粮

微信打赏

菜单