forked from daixianceng/yii2-smser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudSmser.php
70 lines (58 loc) · 1.72 KB
/
CloudSmser.php
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
<?php
namespace daixianceng\smser;
use yii\base\InvalidConfigException;
/**
* 中国云信
*
* @author Cosmo <daixianceng@gmail.com>
* @property string $password write-only password
* @property string $state read-only state
* @property string $message read-only message
*/
class CloudSmser extends Smser
{
/**
* @inheritdoc
*/
public $url = 'http://api.sms.cn/mtutf8/';
/**
* @inheritdoc
*/
public function send($mobile, $content)
{
if (parent::send($mobile, $content)) {
return true;
}
$data = [
'uid' => $this->username,
'pwd' => $this->password,
'mobile' => $mobile,
'content' => $content
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
curl_close($ch);
$resultArr = [];
parse_str($result, $resultArr);
$this->state = isset($resultArr['stat']) ? (string) $resultArr['stat'] : null;
$this->message = isset($resultArr['message']) ? (string) $resultArr['message'] : null;
return $this->state === '100';
}
/**
* 设置密码
*
* @param string $password
* @throws InvalidConfigException
*/
public function setPassword($password)
{
if ($this->username === null) {
throw new InvalidConfigException('用户名不能为空');
}
$this->password = md5($password . $this->username);
}
}