|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/easy-sms. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Overtrue\EasySms\Gateways; |
|
13
|
|
|
|
|
14
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
|
15
|
|
|
use Overtrue\EasySms\Contracts\PhoneNumberInterface; |
|
16
|
|
|
use Overtrue\EasySms\Exceptions\GatewayErrorException; |
|
17
|
|
|
use Overtrue\EasySms\Support\Config; |
|
18
|
|
|
use Overtrue\EasySms\Traits\HasHttpRequest; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class JuheGateway. |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://www.juhe.cn/docs/api/id/54 |
|
24
|
|
|
*/ |
|
25
|
|
|
class JuheGateway extends Gateway |
|
26
|
|
|
{ |
|
27
|
|
|
use HasHttpRequest; |
|
28
|
|
|
|
|
29
|
|
|
const ENDPOINT_URL = 'http://v.juhe.cn/sms/send'; |
|
30
|
|
|
|
|
31
|
|
|
const ENDPOINT_FORMAT = 'json'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to |
|
35
|
|
|
* @param \Overtrue\EasySms\Contracts\MessageInterface $message |
|
36
|
|
|
* @param \Overtrue\EasySms\Support\Config $config |
|
37
|
1 |
|
* |
|
38
|
|
|
* @return array |
|
39
|
1 |
|
* |
|
40
|
|
|
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ; |
|
41
|
|
|
*/ |
|
42
|
|
|
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) |
|
43
|
|
|
{ |
|
44
|
|
|
$params = [ |
|
45
|
|
|
'mobile' => $to->getNumber(), |
|
46
|
|
|
'tpl_id' => $message->getTemplate($this), |
|
47
|
|
|
'tpl_value' => $this->formatTemplateVars($message->getData($this)), |
|
48
|
|
|
'dtype' => self::ENDPOINT_FORMAT, |
|
49
|
|
|
'key' => $config->get('app_key'), |
|
50
|
|
|
]; |
|
51
|
1 |
|
|
|
52
|
|
|
$result = $this->get(self::ENDPOINT_URL, $params); |
|
53
|
|
|
|
|
54
|
1 |
|
if ($result['error_code']) { |
|
55
|
1 |
|
throw new GatewayErrorException($result['reason'], $result['error_code'], $result); |
|
56
|
1 |
|
} |
|
57
|
1 |
|
|
|
58
|
1 |
|
return $result; |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
/** |
|
62
|
|
|
* @param array $vars |
|
63
|
1 |
|
* |
|
64
|
1 |
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function formatTemplateVars(array $vars) |
|
67
|
1 |
|
{ |
|
68
|
|
|
$formatted = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach ($vars as $key => $value) { |
|
71
|
|
|
$formatted[sprintf('#%s#', trim($key, '#'))] = $value; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return http_build_query($formatted); |
|
75
|
1 |
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|