|
本帖最后由 三零网科 于 2017-9-7 11:45 编辑
PHP复制代码 <?php
/**
* @file
* @author SanLingNet <202015066@qq.com>
* @version 1.0,20170907
* @package 阿里大于
* $this->load->library('Dysms');
* $this->dysms->to('17071560232')->data('number','2222')->sendsms();
* 阿里大于接口
*/
class Dysms {
const API_HOST = 'https://dysmsapi.aliyuncs.com/';
/**
* 配置信息
*/
protected $config = array(
'accessKeyId' => 'xxx',
'SignName' => '云计费',//短信签名
'accessKeySecret' => 'xxxx',
);
protected $smsData = array(
'to' => null,
'data' => array(),
);
/**
* 析构函数
*/
public function __construct ($config = array())
{
if (is_array($config)) {
$this->config = $config + $this->config;
}
}
/**
* 获取某项配置
*/
public function getConfig ($name, $default = null)
{
return isset($this->config[$name]) ? $this->config[$name] : $default;
}
/**
* 发送号码
* @param [type] $mobile [description]
* @return [type] [description]
*/
public function to ($mobile)
{
if (is_string($mobile)) {
$mobile = trim($mobile);
}
$this->smsData['to'] = $mobile;
return $this;
}
/**
* 内容
* @param [type] $key [description]
* @param [type] $value [description]
* @return [type] [description]
*/
public function data ($key, $value = null)
{
$this->smsData['data'] = array($key => $value);
return $this;
}
public function sendSms ()
{
$param = [
'Action' => 'SendSms',
'PhoneNumbers' => $this->smsData['to'],
'SignName' => $this->config['SignName'],
'TemplateCode' => 'SMS_80115042',//短信模版code
'TemplateParam' => $this->getTempDataString($this->smsData['data'])//"{'number':'111'}",
];
$jsonArr = $this->call('alibaba.aliqin.fc.sms.num.send', $param);
if ($jsonArr)
{
if ($jsonArr['Code'] == 'OK')
{
$response = array(
'status' => true,
'errorMessage' => '发送成功'
);
}
else
{
$response = array(
'status' => false,
'errorMessage' => self::error($jsonArr['Code'])
);
}
}
else
{
$response = array(
'status' => false,
'errorMessage' => '短信接口异常'
);
}
return json_encode($response);
}
/** * 封装接口请求
*/
public function call ($method, $param)
{
$param = $this->mergeParam($method, $param);
$body = self::http(self::API_HOST, $param,'POST');
if ($jsonArr = json_decode($body, true))
{
return $jsonArr;
}
else
{
return false;
}
}
/**
* [http description]
* @param [type] $url [请求url]
* @param array $params [请求参数]
* @param string $method [请求方法]
* @return [type] [description]
*/
private function http ($url,$params=array(),$method='GET')
{
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
);
/* 根据请求类型设置特定参数 */
switch(strtoupper($method)){
case 'GET':
$getQuerys = !empty($params) ? '?'. http_build_query($params) : '';
$opts[CURLOPT_URL ] = $url . $getQuerys;
break;
case 'POST':
$opts[CURLOPT_URL ] = $url;
$opts[CURLOPT_POST ] = 1;
$opts[CURLOPT_POSTFIELDS ] = http_build_query($params);
break;
}
/* 初始化并执行curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);
if ($err > 0) {
return false;
}else {
return $data;
}
}
//合并系统参数
protected function mergeParam ($method, $param) {
$param += array(
'AccessKeyId' => $this->config['accessKeyId'],
'RegionId' => 'cn-hangzhou',
'Format' => 'JSON',
'Version' => '2017-05-25',
'SignatureMethod' => 'HMAC-SHA1',
'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
'SignatureVersion' => '1.0',
'SignatureNonce' => uniqid(),
);
$param['Signature'] = $this->computeSignature($param);
return $param;
}
private function computeSignature ($parameters)
{
ksort($parameters);
$canonicalizedQueryString = '';
foreach ($parameters as $key => $value) {
$canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
}
$stringToSign = 'POST&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
return base64_encode(hash_hmac('sha1', $stringToSign, $this->config['accessKeySecret'] . '&', true));
}
protected function percentEncode ($str)
{
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
protected function getTempDataString (array $data)
{
$data = array_map(function ($value) {
return (string ) $value;
}, $data);
return json_encode($data);
}
/**
* 错误提示信息
* @param [type] $errMsg [description]
* @return [type] [description]
*/
static function error ($errMsg = null)
{
switch ($errMsg) {
case 'isp.RAM_PERMISSION_DENY' : return 'RAM权限DENY';
case 'isv.OUT_OF_SERVICE' : return '业务停机';
case 'isv.PRODUCT_UN_SUBSCRIPT' : return '未开通云通信产品的阿里云客户';
case 'isv.PRODUCT_UNSUBSCRIBE' : return '产品未开通';
case 'isv.ACCOUNT_NOT_EXISTS' : return '账户不存在';
case 'isv.ACCOUNT_ABNORMAL' : return '账户异常';
case 'isv.SMS_TEMPLATE_ILLEGAL' : return '短信模板不合法';
case 'isv.SMS_SIGNATURE_ILLEGAL' : return '短信签名不合法';
case 'isv.INVALID_PARAMETERS' : return '参数异常';
case 'isp.SYSTEM_ERROR' : return '系统错误';
case 'isv.MOBILE_NUMBER_ILLEGAL' : return '非法手机号';
case 'sv.MOBILE_COUNT_OVER_LIMIT' : return '手机号码数量超过限制';
case 'isv.TEMPLATE_MISSING_PARAMETERS': return '模板缺少变量';
case 'isv.BUSINESS_LIMIT_CONTROL' : return '业务限流';
case 'isv.INVALID_JSON_PARAM' : return 'JSON参数不合法,只接受字符串值';
case 'isv.BLACK_KEY_CONTROL_LIMIT' : return '黑名单管控';
case 'isv.PARAM_LENGTH_LIMIT' : return '参数超出长度限制';
case 'isv.PARAM_NOT_SUPPORT_URL' : return '不支持URL';
case 'isv.AMOUNT_NOT_ENOUGH' : return '账户余额不足';
default : return '未知错误';
}
}
}
复制代码
|
|