|
@@ -13,6 +13,7 @@ namespace Api; |
|
@@ -13,6 +13,7 @@ namespace Api; |
13
|
|
13
|
|
14
|
class Yohobuy
|
14
|
class Yohobuy
|
15
|
{
|
15
|
{
|
|
|
16
|
+
|
16
|
const API_URL = 'http://api2.open.yohobuy.com/';
|
17
|
const API_URL = 'http://api2.open.yohobuy.com/';
|
17
|
const SERVICE_URL = 'http://service.api.yohobuy.com/';
|
18
|
const SERVICE_URL = 'http://service.api.yohobuy.com/';
|
18
|
|
19
|
|
|
@@ -26,11 +27,11 @@ class Yohobuy |
|
@@ -26,11 +27,11 @@ class Yohobuy |
26
|
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
|
27
|
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
|
27
|
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
|
28
|
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
|
28
|
);
|
29
|
);
|
29
|
-
|
30
|
+
|
30
|
/**
|
31
|
/**
|
31
|
* 取得当前的客户端类型
|
32
|
* 取得当前的客户端类型
|
32
|
*/
|
33
|
*/
|
33
|
- public static function clientType()
|
34
|
+ public static function clientType()
|
34
|
{
|
35
|
{
|
35
|
// 苹果设备
|
36
|
// 苹果设备
|
36
|
if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
|
37
|
if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
|
|
@@ -45,7 +46,7 @@ class Yohobuy |
|
@@ -45,7 +46,7 @@ class Yohobuy |
45
|
return 'android';
|
46
|
return 'android';
|
46
|
}
|
47
|
}
|
47
|
}
|
48
|
}
|
48
|
-
|
49
|
+
|
49
|
/**
|
50
|
/**
|
50
|
* 取得公共的参数
|
51
|
* 取得公共的参数
|
51
|
*
|
52
|
*
|
|
@@ -64,7 +65,7 @@ class Yohobuy |
|
@@ -64,7 +65,7 @@ class Yohobuy |
64
|
);
|
65
|
);
|
65
|
return $param;
|
66
|
return $param;
|
66
|
}
|
67
|
}
|
67
|
-
|
68
|
+
|
68
|
/**
|
69
|
/**
|
69
|
* 构建URL
|
70
|
* 构建URL
|
70
|
*
|
71
|
*
|
|
@@ -74,6 +75,10 @@ class Yohobuy |
|
@@ -74,6 +75,10 @@ class Yohobuy |
74
|
*/
|
75
|
*/
|
75
|
public static function httpBuildQuery($url, $data)
|
76
|
public static function httpBuildQuery($url, $data)
|
76
|
{
|
77
|
{
|
|
|
78
|
+ // 销毁私钥参数
|
|
|
79
|
+ if (isset($data['private_key'])) {
|
|
|
80
|
+ unset($data['private_key']);
|
|
|
81
|
+ }
|
77
|
if (strstr($url, '?') !== false) {
|
82
|
if (strstr($url, '?') !== false) {
|
78
|
$url .= '&' . http_build_query($data, null, '&');
|
83
|
$url .= '&' . http_build_query($data, null, '&');
|
79
|
} else {
|
84
|
} else {
|
|
@@ -81,7 +86,7 @@ class Yohobuy |
|
@@ -81,7 +86,7 @@ class Yohobuy |
81
|
}
|
86
|
}
|
82
|
return $url;
|
87
|
return $url;
|
83
|
}
|
88
|
}
|
84
|
-
|
89
|
+
|
85
|
/**
|
90
|
/**
|
86
|
* get方式调用接口
|
91
|
* get方式调用接口
|
87
|
*
|
92
|
*
|
|
@@ -93,13 +98,14 @@ class Yohobuy |
|
@@ -93,13 +98,14 @@ class Yohobuy |
93
|
*/
|
98
|
*/
|
94
|
public static function get($url, $data = array(), $returnJson = false, $timeout = 5)
|
99
|
public static function get($url, $data = array(), $returnJson = false, $timeout = 5)
|
95
|
{
|
100
|
{
|
|
|
101
|
+ // 销毁私钥参数
|
96
|
if (isset($data['private_key'])) {
|
102
|
if (isset($data['private_key'])) {
|
97
|
unset($data['private_key']);
|
103
|
unset($data['private_key']);
|
98
|
}
|
104
|
}
|
99
|
if (!empty($data)) {
|
105
|
if (!empty($data)) {
|
100
|
$url = self::httpBuildQuery($url, $data);
|
106
|
$url = self::httpBuildQuery($url, $data);
|
101
|
}
|
107
|
}
|
102
|
-
|
108
|
+
|
103
|
$ch = curl_init($url);
|
109
|
$ch = curl_init($url);
|
104
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
110
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
105
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
111
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
@@ -128,11 +134,12 @@ class Yohobuy |
|
@@ -128,11 +134,12 @@ class Yohobuy |
128
|
public static function post($url, $data = array(), $returnJson = false, $timeout = 5, $header = array(), $cookie = array())
|
134
|
public static function post($url, $data = array(), $returnJson = false, $timeout = 5, $header = array(), $cookie = array())
|
129
|
{
|
135
|
{
|
130
|
$ch = curl_init($url);
|
136
|
$ch = curl_init($url);
|
131
|
-
|
137
|
+
|
132
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
138
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
133
|
if (!empty($header)) {
|
139
|
if (!empty($header)) {
|
134
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
140
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
135
|
- } else {
|
141
|
+ }
|
|
|
142
|
+ else {
|
136
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
143
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
137
|
}
|
144
|
}
|
138
|
|
145
|
|
|
@@ -146,6 +153,7 @@ class Yohobuy |
|
@@ -146,6 +153,7 @@ class Yohobuy |
146
|
|
153
|
|
147
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
154
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
148
|
curl_setopt($ch, CURLOPT_POST, true);
|
155
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
156
|
+ // 销毁私钥参数
|
149
|
if (isset($data['private_key'])) {
|
157
|
if (isset($data['private_key'])) {
|
150
|
unset($data['private_key']);
|
158
|
unset($data['private_key']);
|
151
|
}
|
159
|
}
|
|
@@ -158,7 +166,7 @@ class Yohobuy |
|
@@ -158,7 +166,7 @@ class Yohobuy |
158
|
}
|
166
|
}
|
159
|
curl_close($ch);
|
167
|
curl_close($ch);
|
160
|
$data = array();
|
168
|
$data = array();
|
161
|
-
|
169
|
+
|
162
|
return $result;
|
170
|
return $result;
|
163
|
}
|
171
|
}
|
164
|
|
172
|
|
|
@@ -173,14 +181,15 @@ class Yohobuy |
|
@@ -173,14 +181,15 @@ class Yohobuy |
173
|
{
|
181
|
{
|
174
|
$result = array();
|
182
|
$result = array();
|
175
|
$response = array();
|
183
|
$response = array();
|
176
|
- $running = null;
|
184
|
+ $running = 0;
|
177
|
$data = '';
|
185
|
$data = '';
|
178
|
$error = '';
|
186
|
$error = '';
|
179
|
$defaultOptions = array(
|
187
|
$defaultOptions = array(
|
180
|
CURLOPT_HEADER => 0,
|
188
|
CURLOPT_HEADER => 0,
|
181
|
CURLOPT_RETURNTRANSFER => 1,
|
189
|
CURLOPT_RETURNTRANSFER => 1,
|
182
|
- CURLOPT_CONNECTTIMEOUT => 5,
|
|
|
183
|
- CURLOPT_TIMEOUT => 5,
|
190
|
+ CURLOPT_CONNECTTIMEOUT => 3,
|
|
|
191
|
+ CURLOPT_TIMEOUT => 3,
|
|
|
192
|
+ CURLOPT_NOSIGNAL => 1, //忽略所有的curl传递给php的信号,减少并发crash
|
184
|
);
|
193
|
);
|
185
|
$mh = curl_multi_init();
|
194
|
$mh = curl_multi_init();
|
186
|
$ch = array();
|
195
|
$ch = array();
|
|
@@ -201,17 +210,21 @@ class Yohobuy |
|
@@ -201,17 +210,21 @@ class Yohobuy |
201
|
}
|
210
|
}
|
202
|
|
211
|
|
203
|
// 调用API接口
|
212
|
// 调用API接口
|
204
|
- do {
|
|
|
205
|
- $status = curl_multi_exec($mh, $running);
|
|
|
206
|
- }
|
|
|
207
|
- while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
208
|
- while ($running && $status == CURLM_OK) {
|
|
|
209
|
- if (curl_multi_select($mh, 0.5) > -1) {
|
|
|
210
|
- do {
|
|
|
211
|
- $status = curl_multi_exec($mh, $running);
|
|
|
212
|
- } while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
213
|
- }
|
213
|
+ do {
|
|
|
214
|
+ do {
|
|
|
215
|
+ $status = curl_multi_exec($mh, $running);
|
|
|
216
|
+ }
|
|
|
217
|
+ while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
218
|
+
|
|
|
219
|
+ if ($status != CURLM_OK) {
|
|
|
220
|
+ break;
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+ if ($running > 0) {
|
|
|
224
|
+ curl_multi_select($mh, 0.5);
|
|
|
225
|
+ }
|
214
|
}
|
226
|
}
|
|
|
227
|
+ while ($running);
|
215
|
|
228
|
|
216
|
// 获取API接口响应的结果
|
229
|
// 获取API接口响应的结果
|
217
|
foreach ($urlList as $name => $api) {
|
230
|
foreach ($urlList as $name => $api) {
|
|
@@ -235,26 +248,49 @@ class Yohobuy |
|
@@ -235,26 +248,49 @@ class Yohobuy |
235
|
curl_close($ch[$name]);
|
248
|
curl_close($ch[$name]);
|
236
|
}
|
249
|
}
|
237
|
curl_multi_close($mh);
|
250
|
curl_multi_close($mh);
|
238
|
-
|
251
|
+
|
239
|
return $result;
|
252
|
return $result;
|
240
|
}
|
253
|
}
|
241
|
-
|
254
|
+
|
242
|
/**
|
255
|
/**
|
243
|
* rpc调用远程服务(YAR)
|
256
|
* rpc调用远程服务(YAR)
|
244
|
*
|
257
|
*
|
245
|
* @see http://php.net/manual/zh/yar-client.setopt.php
|
258
|
* @see http://php.net/manual/zh/yar-client.setopt.php
|
246
|
* @return array
|
259
|
* @return array
|
247
|
*/
|
260
|
*/
|
248
|
- public static function yarClient($uri, $method, $data = array(), $timeout = 3000)
|
261
|
+ public static function yarClient($uri, $method, $parameters = array(), $timeout = 3000)
|
249
|
{
|
262
|
{
|
250
|
$client = new \Yar_Client($uri);
|
263
|
$client = new \Yar_Client($uri);
|
251
|
$client->SetOpt(YAR_OPT_PACKAGER, 'php');
|
264
|
$client->SetOpt(YAR_OPT_PACKAGER, 'php');
|
252
|
$client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
|
265
|
$client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
|
253
|
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
|
266
|
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
|
254
|
-
|
|
|
255
|
- $result = $client->$method($data);
|
|
|
256
|
-
|
267
|
+ $result = $client->$method($parameters);
|
|
|
268
|
+
|
257
|
return $result;
|
269
|
return $result;
|
258
|
}
|
270
|
}
|
|
|
271
|
+
|
|
|
272
|
+ /**
|
|
|
273
|
+ * 并行(异步)调用远程服务
|
|
|
274
|
+ *
|
|
|
275
|
+ * @see http://php.net/manual/zh/class.yar-concurrent-client.php
|
|
|
276
|
+ * @param string $uri
|
|
|
277
|
+ * @param string $method
|
|
|
278
|
+ * @param array $parameter
|
|
|
279
|
+ * @param callable $callback
|
|
|
280
|
+ * @param int $timeout
|
|
|
281
|
+ * @return void
|
|
|
282
|
+ */
|
|
|
283
|
+ public static function yarConcurrentCall($uri, $method, $parameters, $callback, $timeout = 3000)
|
|
|
284
|
+ {
|
|
|
285
|
+ \Yar_Concurrent_Client::call($uri, $method, array($parameters), $callback, null, array(
|
|
|
286
|
+ YAR_OPT_PACKAGER => 'php',
|
|
|
287
|
+ YAR_OPT_TIMEOUT => $timeout,
|
|
|
288
|
+ YAR_OPT_CONNECT_TIMEOUT => $timeout
|
|
|
289
|
+ ));
|
|
|
290
|
+ }
|
|
|
291
|
+ public static function yarConcurrentLoop($callback = null)
|
|
|
292
|
+ {
|
|
|
293
|
+ \Yar_Concurrent_Client::loop($callback);
|
|
|
294
|
+ }
|
259
|
|
295
|
|
260
|
} |
296
|
} |