forked from daixianceng/yii2-smser
/
YunpianSmser.php
55 lines (45 loc) · 1.3 KB
/
YunpianSmser.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
<?php
namespace daixianceng\smser;
use yii\base\InvalidConfigException;
/**
* 云片网
*
* @author Cosmo <daixianceng@gmail.com>
* @property string $state read-only state
* @property string $message read-only message
*/
class YunpianSmser extends Smser
{
public $apikey;
/**
* @inheritdoc
*/
public $url = 'http://yunpian.com/v1/sms/send.json';
/**
* @inheritdoc
*/
public function send($mobile, $content)
{
if (parent::send($mobile, $content)) {
return true;
}
$data = [
'apikey' => $this->apikey,
'mobile' => $mobile,
'text' => $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);
$json = json_decode($result);
if ($json && is_object($json)) {
$this->state = isset($json->code) ? (string) $json->code : null;
$this->message = isset($json->msg) ? (string) $json->msg : null;
}
return $this->state === '0';
}
}