<?php
namespace app\admin\controller;
/**
* 阿里云短信发送接口
*/
class Alisms
{
public $accesskeyid = "";
public $accesskeysecret = '';
public $gatewayUrl = "http://dysmsapi.aliyuncs.com/";
public $format = "JSON";
protected $signatureMethod = "HMAC-SHA1";
protected $regionId = "cn-hangzhou";
protected $signatureVersion = "1.0";
protected $version = "2017-05-25";
protected $action = "SendSms";
public function __construct($accesskeyid = "",$accesskeysecret = "")
{
if($accesskeyid&&$accesskeysecret){
$this->accesskeyid = $accesskeyid;
$this->accesskeysecret = $accesskeysecret ;
}
}
protected function generateSign($params)
{
ksort($params);
$stringToSign = "";
foreach ($params as $k => $v){
$stringToSign .= "&"."$k=" . urlencode($v);
}
$stringToSign = "POST&%2F&".urlencode(substr($stringToSign, 1));
return base64_encode(hash_hmac('sha1', $stringToSign, $this->accesskeysecret."&", true));
}
public function curl($url, $postFields = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$reponse = curl_exec($ch);
curl_close($ch);
return $reponse;
}
public function sendSms()
{
$sysParams["AccessKeyId"] = $this->accesskeyid;
$sysParams["RegionId"] = $this->regionId;
$sysParams["Format"] = $this->format;
$sysParams["SignatureMethod"] = $this->signatureMethod;
$sysParams["SignatureVersion"] = $this->signatureVersion;
$sysParams["Action"] = $this->action;
$sysParams["Version"] = $this->version;
date_default_timezone_set("GMT");
$sysParams["SignatureNonce"] = uniqid();
$sysParams["Timestamp"] = date("Y-m-d\TH:i:s\Z");
$sysParams["Signature"] = $this->generateSign(array_merge($sysParams));
$resp = $this->curl($this->gatewayUrl, array_merge($sysParams));
$respObject = json_decode($resp, true);
return $respObject;
}
}
?>