Sendcloud.class.php
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* @api sendcloud php对接短信接口
*/
namespace Com\Sms;
class Sendcloud
{
/** 账号信息 */
protected $_account = '';
/** 密码 */
protected $_passwd = '';
/** 短信接口地址 */
const SEND_BATCH_URL = 'http://sendcloud.sohu.com/smsapi/send';
/**
* &instance
* 获取一个短信发送类的实例
*
* @return object
*/
public static function &instance($account = null, $password = null)
{
static $instance = null;
if (! $instance) {
$instance = new Sendcloud($account, $password);
}
return $instance;
}
/**
* __construct
*
* @param mixed $group
* @return void
*/
public function __construct($account = null, $password = null)
{
$this->_account = $account === null ? cfg('SMS_SENDCLOUD.ACCOUNT') : $account;
$this->_passwd = $password === null ? cfg('SMS_SENDCLOUD.PASSWORD') : $password;
}
/**
* 发送短信
*
* @param string $tplname
* 模板名称
* @param string $mobiles
* 手机号码
* @param string $vars
* 变量名称
* @param int $type
* 类型名称
*/
public function send(&$result, $tplname, $mobiles, $vars, $type = 0)
{
$sig = '';
// 请求参数
$params = array(
'smsUser' => $this->_account,
'templateId' => (string) $tplname,
'msgType' => (string) $type,
'phone' => is_array($mobiles) ? implode('|', $mobiles) : $mobiles,
'vars' => json_encode($vars)
);
// 生成密钥
$this->_generate_signature($sig, $params);
$params['signature'] = $sig;
// 生成请求数据
$data = http_build_query($params);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type:application/x-www-form-urlencoded',
'content' => $data
)
);
// 请求接口
$context = stream_context_create($options);
$result = file_get_contents(self::SEND_BATCH_URL, FILE_TEXT, $context);
// 记录发送日志
\Think\Log::record('params:' . var_export($params, true));
// 判断返回值
if (! $this->_check_result($result)) {
return false;
}
return true;
}
/**
* 生成签名
*
* @param string $sig
* 签名
* @param array $params
* 签名所必须的参数
* @return boolean
*/
protected function _generate_signature(&$sig, $params)
{
$param_str = "";
ksort($params);
foreach ($params as $_key => $_value) {
$param_str .= $_key . '=' . $_value . '&';
}
$param_str = trim($param_str, '&');
$sig = md5($this->_passwd . "&" . $param_str . "&" . $this->_passwd);
return true;
}
/**
* 判断返回值
*
* @param string $result
* 接口返回值
*/
protected function _check_result($result)
{
$res = json_decode($result, true);
// 如果返回错误
if (null === $res || ! isset($res['statusCode']) || 200 != $res['statusCode']) {
\Think\Log::record('send error, result:' . $result);
return false;
}
\Think\Log::record('send ok, result:' . $result);
return true;
}
}