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 AliyunrestGateway. |
22
|
|
|
*/ |
23
|
|
|
class AliyunrestGateway extends Gateway |
24
|
|
|
{ |
25
|
|
|
use HasHttpRequest; |
26
|
|
|
|
27
|
|
|
const ENDPOINT_URL = 'http://gw.api.taobao.com/router/rest'; |
28
|
|
|
|
29
|
|
|
const ENDPOINT_VERSION = '2.0'; |
30
|
|
|
|
31
|
|
|
const ENDPOINT_FORMAT = 'json'; |
32
|
|
|
|
33
|
|
|
const ENDPOINT_METHOD = 'alibaba.aliqin.fc.sms.num.send'; |
34
|
|
|
|
35
|
|
|
const ENDPOINT_SIGNATURE_METHOD = 'md5'; |
36
|
|
|
|
37
|
|
|
const ENDPOINT_PARTNER_ID = 'EasySms'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param PhoneNumberInterface $to |
41
|
|
|
* @param MessageInterface $message |
42
|
|
|
* @param Config $config |
43
|
|
|
* |
44
|
|
|
* @return array|void |
|
|
|
|
45
|
|
|
*/ |
46
|
1 |
|
public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config) |
47
|
|
|
{ |
48
|
|
|
$urlParams = [ |
49
|
1 |
|
'app_key' => $config->get('app_key'), |
50
|
1 |
|
'v' => self::ENDPOINT_VERSION, |
51
|
1 |
|
'format' => self::ENDPOINT_FORMAT, |
52
|
1 |
|
'sign_method' => self::ENDPOINT_SIGNATURE_METHOD, |
53
|
1 |
|
'method' => self::ENDPOINT_METHOD, |
54
|
1 |
|
'timestamp' => date('Y-m-d H:i:s'), |
55
|
1 |
|
'partner_id' => self::ENDPOINT_PARTNER_ID, |
56
|
1 |
|
]; |
57
|
|
|
|
58
|
|
|
$params = [ |
|
|
|
|
59
|
1 |
|
'extend' => '', |
60
|
1 |
|
'sms_type' => 'normal', |
61
|
1 |
|
'sms_free_sign_name' => $config->get('sign_name'), |
62
|
1 |
|
'sms_param' => json_encode($message->getData()), |
63
|
1 |
|
'rec_num' => !\is_null($to->getIDDCode()) ? strval($to->getZeroPrefixedNumber()) : $to->getNumber(), |
64
|
1 |
|
'sms_template_code' => $message->getTemplate($this), |
65
|
1 |
|
]; |
66
|
1 |
|
$urlParams['sign'] = $this->generateSign(array_merge($params, $urlParams)); |
67
|
|
|
|
68
|
1 |
|
$result = $this->post($this->getEndpointUrl($urlParams), $params); |
69
|
|
|
|
70
|
1 |
|
if (isset($result['error_response']) && $result['error_response']['code'] != 0) { |
71
|
1 |
|
throw new GatewayErrorException($result['error_response']['msg'], $result['error_response']['code'], $result); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $params |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
protected function getEndpointUrl($params) |
83
|
|
|
{ |
84
|
1 |
|
return self::ENDPOINT_URL.'?'.http_build_query($params); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $params |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
1 |
|
protected function generateSign($params) |
93
|
|
|
{ |
94
|
1 |
|
ksort($params); |
95
|
|
|
|
96
|
1 |
|
$stringToBeSigned = $this->config->get('app_secret_key'); |
97
|
1 |
|
foreach ($params as $k => $v) { |
98
|
1 |
|
if (!is_array($v) && '@' != substr($v, 0, 1)) { |
99
|
1 |
|
$stringToBeSigned .= "$k$v"; |
|
|
|
|
100
|
1 |
|
} |
101
|
1 |
|
} |
102
|
1 |
|
unset($k, $v); |
103
|
1 |
|
$stringToBeSigned .= $this->config->get('app_secret_key'); |
104
|
|
|
|
105
|
1 |
|
return strtoupper(md5($stringToBeSigned)); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.