first commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace lib\sms;
|
||||
|
||||
class Aliyun
|
||||
{
|
||||
private $AccessKeyId;
|
||||
private $AccessKeySecret;
|
||||
|
||||
function __construct($AccessKeyId, $AccessKeySecret)
|
||||
{
|
||||
$this->AccessKeyId = $AccessKeyId;
|
||||
$this->AccessKeySecret = $AccessKeySecret;
|
||||
}
|
||||
private function aliyunSignature($parameters, $accessKeySecret, $method)
|
||||
{
|
||||
ksort($parameters);
|
||||
$canonicalizedQueryString = '';
|
||||
foreach ($parameters as $key => $value) {
|
||||
if($value === null) continue;
|
||||
$canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
|
||||
}
|
||||
$stringToSign = $method . '&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
|
||||
$signature = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&", true));
|
||||
|
||||
return $signature;
|
||||
}
|
||||
private function percentEncode($str)
|
||||
{
|
||||
$search = ['+', '*', '%7E'];
|
||||
$replace = ['%20', '%2A', '~'];
|
||||
return str_replace($search, $replace, urlencode($str));
|
||||
}
|
||||
public function send($phone, $param, $moban, $sign, $sitename)
|
||||
{
|
||||
if (empty($this->AccessKeyId) || empty($this->AccessKeySecret)) return false;
|
||||
$url = 'https://dysmsapi.aliyuncs.com/';
|
||||
$TemplateParam = json_encode($param);
|
||||
$data = array(
|
||||
'Action' => 'SendSms',
|
||||
'PhoneNumbers' => $phone,
|
||||
'SignName' => $sign,
|
||||
'TemplateCode' => $moban,
|
||||
'TemplateParam' => $TemplateParam,
|
||||
'Format' => 'JSON',
|
||||
'RegionId' => 'cn-hangzhou',
|
||||
'Version' => '2017-05-25',
|
||||
'AccessKeyId' => $this->AccessKeyId,
|
||||
'SignatureMethod' => 'HMAC-SHA1',
|
||||
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
|
||||
'SignatureVersion' => '1.0',
|
||||
'SignatureNonce' => random(8)
|
||||
);
|
||||
$data['Signature'] = $this->aliyunSignature($data, $this->AccessKeySecret, 'POST');
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
||||
$json = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
$arr = json_decode($json, true);
|
||||
return $arr;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace lib\sms;
|
||||
|
||||
class Qcloud
|
||||
{
|
||||
private static $apiurl = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms';
|
||||
private $appid;
|
||||
private $appkey;
|
||||
|
||||
public function __construct($appid, $appkey)
|
||||
{
|
||||
$this->appid = $appid;
|
||||
$this->appkey = $appkey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定模板单发短信
|
||||
* @param string $mobile 手机号码
|
||||
* @param string $tpl_id 模板ID
|
||||
* @param array $params 模板参数
|
||||
* @param string $sign 签名内容
|
||||
* @return bool
|
||||
*/
|
||||
public function send($mobile, $tpl_id, $params, $sign)
|
||||
{
|
||||
if (empty($this->appid) || empty($this->appkey)) return false;
|
||||
$time = time();
|
||||
$random = rand(100000, 999999);
|
||||
$url = self::$apiurl . "?sdkappid=" . $this->appid . "&random=" . $random;
|
||||
$data = [
|
||||
'tel' => [
|
||||
'nationcode' => '86',
|
||||
'mobile' => $mobile
|
||||
],
|
||||
'params' => $params,
|
||||
'time' => $time,
|
||||
'tpl_id' => intval($tpl_id),
|
||||
'sign' => $sign,
|
||||
'sig' => $this->getSig($random, $time, $mobile),
|
||||
];
|
||||
try {
|
||||
$res = $this->curlPost($url, json_encode($data));
|
||||
$arr = json_decode($res, true);
|
||||
return $arr;
|
||||
} catch (\Exception $e) {
|
||||
return ['result' => -1, 'errmsg' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
//生成签名
|
||||
private function getSig($random, $time, $mobile)
|
||||
{
|
||||
$signstr = 'appkey=' . $this->appkey . '&random=' . $random . '&time=' . $time . '&mobile=' . $mobile;
|
||||
return hash("sha256", $signstr);
|
||||
}
|
||||
|
||||
//发起POST请求
|
||||
private function curlPost($url, $data)
|
||||
{
|
||||
$httpheader[] = "Accept: */*";
|
||||
$httpheader[] = "Content-Type: application/json; charset=utf8";
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
$res = curl_exec($ch);
|
||||
if (curl_errno($ch) > 0) {
|
||||
$errmsg = curl_error($ch);
|
||||
curl_close($ch);
|
||||
throw new \Exception($errmsg);
|
||||
}
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
if ($httpCode != 200) {
|
||||
throw new \Exception('http_code=' . $httpCode);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
namespace lib\sms;
|
||||
|
||||
class SmsBao {
|
||||
private $user;
|
||||
private $pass;
|
||||
|
||||
function __construct($user, $pass){
|
||||
$this->user = $user;
|
||||
$this->pass = $pass;
|
||||
}
|
||||
|
||||
public function send($phone, $param, $moban, $sign){
|
||||
if(empty($this->user)||empty($this->pass))return false;
|
||||
$statusStr = array(
|
||||
"0" => "短信发送成功",
|
||||
"-1" => "参数不全",
|
||||
"-2" => "服务器空间不支持",
|
||||
"30" => "密码错误",
|
||||
"40" => "账号不存在",
|
||||
"41" => "余额不足",
|
||||
"42" => "帐户已过期",
|
||||
"43" => "IP地址限制",
|
||||
"50" => "内容含有敏感词"
|
||||
);
|
||||
foreach($param as $k=>$v){
|
||||
$moban = str_replace('{'.$k.'}',$v,$moban);
|
||||
}
|
||||
$content = '【'.$sign.'】'.$moban;
|
||||
$sendurl = "http://api.smsbao.com/sms?u=".$this->user."&p=".md5($this->pass)."&m=".$phone."&c=".urlencode($content);
|
||||
$result = get_curl($sendurl) ;
|
||||
if ($result == '0'){
|
||||
return true;
|
||||
}else{
|
||||
return isset($statusStr[$result])?$statusStr[$result]:('CODE:'.$result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user