1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Toplan\PhpSms; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class LuosimaoAgent |
7
|
|
|
* |
8
|
|
|
* @property string $apikey |
9
|
|
|
* @property string $voiceApikey |
10
|
|
|
*/ |
11
|
|
|
class LuosimaoAgent extends Agent implements ContentSms |
12
|
|
|
{ |
13
|
|
|
public function sendSms($to, $content, $tempId, array $data) |
14
|
|
|
{ |
15
|
|
|
// 签名必须在最后面 |
16
|
|
|
if ($content && !preg_match('/】$/', $content)) { |
17
|
|
|
preg_match('/【([0-9a-zA-Z\W]+)】/', $content, $matches); |
18
|
|
|
if (isset($matches[0])) { |
19
|
|
|
$content = str_replace($matches[0], '', $content) . $matches[0]; |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
$this->sendContentSms($to, $content); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
public function sendContentSms($to, $content) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$url = 'http://sms-api.luosimao.com/v1/send.json'; |
28
|
|
|
$optData = [ |
29
|
|
|
'mobile' => $to, |
30
|
|
|
'message' => $content, |
31
|
|
|
]; |
32
|
|
|
$result = $this->curl($url, $optData, true, [ |
33
|
|
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
34
|
|
|
CURLOPT_USERPWD => "api:key-$this->apikey", |
35
|
|
|
]); |
36
|
|
|
$this->setResult($result); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function voiceVerify($to, $code, $tempId, array $data) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$url = 'http://voice-api.luosimao.com/v1/verify.json'; |
42
|
|
|
$optData = [ |
43
|
|
|
'mobile' => $to, |
44
|
|
|
'code' => $code, |
45
|
|
|
]; |
46
|
|
|
$result = $this->curl($url, $optData, true, [ |
47
|
|
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
48
|
|
|
CURLOPT_USERPWD => "api:key-$this->voiceApikey", |
49
|
|
|
]); |
50
|
|
|
$this->setResult($result); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
protected function setResult($result) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if ($result['request']) { |
56
|
|
|
$this->result(Agent::INFO, $result['response']); |
57
|
|
|
$result = json_decode($result['response'], true); |
58
|
|
|
$this->result(Agent::SUCCESS, $result['error'] === 0); |
59
|
|
|
$this->result(Agent::CODE, $result['error']); |
60
|
|
|
} else { |
61
|
|
|
$this->result(Agent::INFO, 'request failed'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.