1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the light/apistore. |
5
|
|
|
* |
6
|
|
|
* (c) lichunqiang <[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 light\apistore\apis; |
13
|
|
|
|
14
|
|
|
abstract class Api |
15
|
|
|
{ |
16
|
|
|
public $apikey; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool If Response is json format, Default is true, will decode to array. |
20
|
|
|
*/ |
21
|
|
|
protected $isJsonResponse = true; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
* |
26
|
|
|
* @param string $apikey Api key |
27
|
|
|
*/ |
28
|
17 |
|
public function __construct($apikey) |
29
|
|
|
{ |
30
|
17 |
|
$this->apikey = $apikey; |
31
|
17 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Fetch result. |
35
|
|
|
* |
36
|
|
|
* @param mixed $params |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
abstract public function get($params); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 准备请求头. |
44
|
|
|
* |
45
|
|
|
* @return array 默认的请头 |
46
|
|
|
*/ |
47
|
16 |
|
protected function prepareHeaders() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
16 |
|
'apikey:' . $this->apikey, |
51
|
16 |
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Fetch result by $address. |
56
|
|
|
* |
57
|
|
|
* @param string $address |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
16 |
|
final public function fetch($address) |
62
|
|
|
{ |
63
|
16 |
|
if (function_exists('curl_init')) { |
64
|
16 |
|
$ch = \curl_init(); |
65
|
16 |
|
$header = $this->prepareHeaders(); |
66
|
|
|
// 添加apikey到header |
67
|
16 |
|
\curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
68
|
16 |
|
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
69
|
|
|
// 执行HTTP请求 |
70
|
16 |
|
\curl_setopt($ch, CURLOPT_URL, $address); |
71
|
16 |
|
$res = \curl_exec($ch); |
72
|
16 |
|
} else { |
73
|
|
|
$context = stream_context_create([ |
74
|
|
|
'http' => [ |
75
|
|
|
'header' => implode(';', $this->prepareHeaders()), |
76
|
|
|
], |
77
|
|
|
]); |
78
|
|
|
$res = file_get_contents($address, null, $context); |
79
|
|
|
} |
80
|
|
|
|
81
|
16 |
|
return $this->_parseResponse($res); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Parse response to array. |
86
|
|
|
* |
87
|
|
|
* @param mixed $result |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
11 |
View Code Duplication |
protected function _parseResponse($result) |
|
|
|
|
92
|
|
|
{ |
93
|
11 |
|
if (!$this->isJsonResponse) { |
94
|
2 |
|
return $result; |
95
|
|
|
} |
96
|
9 |
|
$result = json_decode($result, true); |
97
|
9 |
|
if (!$result || !isset($result['errNum'])) { |
98
|
|
|
return ['errcode' => 1, 'errmsg' => 'fetch data error']; |
99
|
|
|
} |
100
|
9 |
|
if ($result['errNum'] == 0) { |
101
|
|
|
return [ |
102
|
6 |
|
'errcode' => 0, |
103
|
6 |
|
'errmsg' => 'success', |
104
|
6 |
|
'data' => $result['retData'], |
105
|
6 |
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
return ['errcode' => $result['errNum'], 'errmsg' => $result['errMsg']]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.