阿里大于短信类库
本帖最后由 三零网科 于 2017-9-7 11:45 编辑<?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 $mobile
* @return
*/
public function to($mobile)
{
if (is_string($mobile)) {
$mobile = trim($mobile);
}
$this->smsData['to'] = $mobile;
return $this;
}
/**
* 内容
* @param $key
* @param $value
* @return
*/
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;
}
}
/**
*
* @param $url [请求url]
* @paramarray$params [请求参数]
* @paramstring $method [请求方法]
* @return
*/
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 = $url . $getQuerys;
break;
case 'POST':
$opts = $url;
$opts = 1;
$opts = 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 $errMsg
* @return
*/
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 '未知错误';
}
}
}
这个不需要指定模板什么的吗?
date('num',222)这个表示什么 你这个只支持传一个变量吗? 我试了 2个 不行了 lePig 发表于 2017-9-9 23:01
这个不需要指定模板什么的吗?
date('num',222)这个表示什么
$this->load->library('Dysms');
$this->dysms->to('17071560232')->data('number','2222')->sendsms();
to是发送的手机号
data('number','2222')表示模版中number变量的值为2222 lePig 发表于 2017-9-9 23:20
你这个只支持传一个变量吗? 我试了 2个 不行了
想多个变量,你改下public function data($key, $value = null)
{
$this->smsData['data'] = array($key => $value);
return $this;
}
改成public function data($data = array())
{
$this->smsData['data'] = $data;
return $this;
}
然后
$this->dysms->to('17071560232')->data(array())->sendsms();就ok了 本帖最后由 lePig 于 2017-9-14 22:24 编辑
三零网科 发表于 2017-9-13 14:26
想多个变量,你改下
改成
改完后发现 下面 这个
{"status":false,"errorMessage":"JSON参数不合法,只接受字符串值"}
您那边测试可以吗?
$this->smsData['data'] = $data;
$data= array() 你是怎么写的
页:
[1]