|
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 function GuzzleHttp\json_encode; |
|
15
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
|
16
|
|
|
use Overtrue\EasySms\Exceptions\GatewayErrorException; |
|
17
|
|
|
use Overtrue\EasySms\Support\Config; |
|
18
|
|
|
use Overtrue\EasySms\Traits\HasHttpRequest; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class ChuanglanGateway. |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://www.253.com/api-docs-1.html |
|
24
|
|
|
*/ |
|
25
|
|
|
class ChuanglanGateway extends Gateway |
|
26
|
|
|
{ |
|
27
|
|
|
use HasHttpRequest; |
|
28
|
|
|
|
|
29
|
|
|
const ENDPOINT_URL = 'https://sms.253.com/msg/send'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array|int|string $to |
|
33
|
|
|
* @param \Overtrue\EasySms\Contracts\MessageInterface $message |
|
34
|
|
|
* @param \Overtrue\EasySms\Support\Config $config |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
* |
|
38
|
|
|
* @throws \Overtrue\EasySms\Exceptions\GatewayErrorException; |
|
39
|
|
|
*/ |
|
40
|
|
|
public function send($to, MessageInterface $message, Config $config) |
|
41
|
|
|
{ |
|
42
|
|
|
$params = [ |
|
43
|
|
|
'un' => $config->get('un'), |
|
44
|
|
|
'pw' => $config->get('pw'), |
|
45
|
|
|
'phone' => $to, |
|
46
|
|
|
'msg' => $message->getContent(), |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
$result = $this->get(self::ENDPOINT_URL, $params); |
|
50
|
|
|
|
|
51
|
|
|
$formatResult = $this->formatResult($result); |
|
52
|
|
|
|
|
53
|
|
|
if ($formatResult[1]) { |
|
54
|
|
|
throw new GatewayErrorException($formatResult[3], $formatResult[1], $result); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $result; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param $result http return from 253 service |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function formatResult($result) |
|
66
|
|
|
{ |
|
67
|
|
|
$result = str_replace("\n", ',', $result); |
|
68
|
|
|
return explode(',', $result); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|