1
|
-<?php
|
|
|
2
|
-
|
|
|
3
|
-/**
|
|
|
4
|
- * 有货相关接口类
|
|
|
5
|
- *
|
|
|
6
|
- * @name Yohobuy
|
|
|
7
|
- * @package library/Api
|
|
|
8
|
- * @copyright yoho.inc
|
|
|
9
|
- * @version 1.0 (2015-9-30 16:42:51)
|
|
|
10
|
- * @author fei.hong <fei.hong@yoho.cn>
|
|
|
11
|
- */
|
|
|
12
|
-
|
|
|
13
|
-namespace Api;
|
|
|
14
|
-
|
|
|
15
|
-use Plugin\Cache;
|
|
|
16
|
-
|
|
|
17
|
-class Yohobuy
|
|
|
18
|
-{
|
|
|
19
|
- /* 正式环境 */
|
|
|
20
|
-
|
|
|
21
|
-// const API_URL = 'http://api2.open.yohobuy.com/';
|
|
|
22
|
-// const API_URL2 = 'http://api.open.yohobuy.com/';
|
|
|
23
|
-// const SERVICE_URL = 'http://service.api.yohobuy.com/';
|
|
|
24
|
-// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
25
|
-
|
|
|
26
|
-// const API_URL = 'http://apih5.yoho.cn/';
|
|
|
27
|
-// const API_URL2 = 'http://apih5.yoho.cn/';
|
|
|
28
|
- // const SERVICE_URL = 'http://serviceh5.yoho.cn/';
|
|
|
29
|
- // const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
30
|
- // const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
31
|
-
|
|
|
32
|
-// /* 测试环境 */
|
|
|
33
|
- // const API_URL = 'http://192.168.102.205:8080/gateway/'; // 先临时使用网关
|
|
|
34
|
- const API_URL = 'http://testapi.yoho.cn:28078/';
|
|
|
35
|
- const SERVICE_URL = 'http://testservice.yoho.cn:28077/';
|
|
|
36
|
- const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
37
|
- const API_OLD = 'http://test2.open.yohobuy.com/';
|
|
|
38
|
-
|
|
|
39
|
- /* 预览环境 */
|
|
|
40
|
-// const API_URL = 'http://preapi.yoho.cn/';
|
|
|
41
|
-// const API_URL2 = 'http://preapi.yoho.cn/';
|
|
|
42
|
-// const SERVICE_URL = 'http://serviceh5.yoho.cn/';
|
|
|
43
|
-// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
44
|
-// const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
45
|
-
|
|
|
46
|
- /* PC重构地址 */
|
|
|
47
|
-// const API_URL = 'http://test.open.yohobuy.com/';
|
|
|
48
|
-// const SERVICE_URL = 'http://test.service.api.yohobuy.com/';
|
|
|
49
|
-// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
50
|
-// const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
51
|
-
|
|
|
52
|
- /**
|
|
|
53
|
- * 私钥列表
|
|
|
54
|
- *
|
|
|
55
|
- * @var array
|
|
|
56
|
- */
|
|
|
57
|
- public static $privateKeyList = array(
|
|
|
58
|
- 'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
|
|
|
59
|
- 'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
|
|
|
60
|
- 'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
|
|
|
61
|
- 'web' => '0ed29744ed318fd28d2c07985d3ba633',
|
|
|
62
|
- );
|
|
|
63
|
-
|
|
|
64
|
- /**
|
|
|
65
|
- * 取得当前的客户端类型
|
|
|
66
|
- */
|
|
|
67
|
- public static function clientType()
|
|
|
68
|
- {
|
|
|
69
|
- // 苹果设备
|
|
|
70
|
- if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
|
|
|
71
|
- return 'iphone';
|
|
|
72
|
- }
|
|
|
73
|
- // 苹果IPAD
|
|
|
74
|
- elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
|
|
|
75
|
-
|
|
|
76
|
- return 'ipad';
|
|
|
77
|
- }
|
|
|
78
|
- elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'android')) {
|
|
|
79
|
- return 'android';
|
|
|
80
|
- }
|
|
|
81
|
- // 其它
|
|
|
82
|
- else {
|
|
|
83
|
- return 'android';
|
|
|
84
|
- }
|
|
|
85
|
- }
|
|
|
86
|
-
|
|
|
87
|
- /**
|
|
|
88
|
- * 取得当前的IP地址
|
|
|
89
|
- *
|
|
|
90
|
- * @param bool $int 返回int类型的ip地址,默认是
|
|
|
91
|
- * @return mixed 当前的IP地址
|
|
|
92
|
- */
|
|
|
93
|
- public static function ip($int = true)
|
|
|
94
|
- {
|
|
|
95
|
- if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']) {
|
|
|
96
|
- $onlineip = $_SERVER['HTTP_CLIENT_IP'];
|
|
|
97
|
- } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
|
|
|
98
|
- $onlineip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
99
|
- } else {
|
|
|
100
|
- $onlineip = $_SERVER['REMOTE_ADDR'];
|
|
|
101
|
- }
|
|
|
102
|
-
|
|
|
103
|
- return $int ? ip2long($onlineip) : $onlineip;
|
|
|
104
|
- }
|
|
|
105
|
-
|
|
|
106
|
- /**
|
|
|
107
|
- * 取得公共的参数
|
|
|
108
|
- *
|
|
|
109
|
- * @return array
|
|
|
110
|
- */
|
|
|
111
|
- public static function param()
|
|
|
112
|
- {
|
|
|
113
|
- $clientType = self::clientType();
|
|
|
114
|
- $param = array(
|
|
|
115
|
- 'app_version' => '3.8.2',
|
|
|
116
|
- 'client_type' => $clientType,
|
|
|
117
|
- 'os_version' => 'yohobuy:h5',
|
|
|
118
|
- 'private_key' => self::$privateKeyList[$clientType],
|
|
|
119
|
- 'screen_size' => '720x1280',
|
|
|
120
|
- 'v' => '7',
|
|
|
121
|
- );
|
|
|
122
|
- return $param;
|
|
|
123
|
- }
|
|
|
124
|
-
|
|
|
125
|
- /**
|
|
|
126
|
- * 构建URL
|
|
|
127
|
- *
|
|
|
128
|
- * @param string $url
|
|
|
129
|
- * @param array $data
|
|
|
130
|
- * @return string
|
|
|
131
|
- */
|
|
|
132
|
- public static function httpBuildQuery($url, $data)
|
|
|
133
|
- {
|
|
|
134
|
- // 销毁私钥参数
|
|
|
135
|
- if (isset($data['private_key'])) {
|
|
|
136
|
- unset($data['private_key']);
|
|
|
137
|
- }
|
|
|
138
|
- if (strstr($url, '?') !== false) {
|
|
|
139
|
- $url .= '&' . http_build_query($data, null, '&');
|
|
|
140
|
- } else {
|
|
|
141
|
- $url .= '?' . http_build_query($data, null, '&');
|
|
|
142
|
- }
|
|
|
143
|
- return $url;
|
|
|
144
|
- }
|
|
|
145
|
-
|
|
|
146
|
- /**
|
|
|
147
|
- * get方式调用接口
|
|
|
148
|
- *
|
|
|
149
|
- * @param string $url 接口URL
|
|
|
150
|
- * @param array $data 参数列表
|
|
|
151
|
- * @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
152
|
- * @param bool $returnJson 控制是否返回json格式数据
|
|
|
153
|
- * @param int $timeout 超时时间
|
|
|
154
|
- * @return mixed
|
|
|
155
|
- */
|
|
|
156
|
- public static function get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5)
|
|
|
157
|
- {
|
|
|
158
|
- // 销毁私钥参数
|
|
|
159
|
- if (isset($data['private_key'])) {
|
|
|
160
|
- unset($data['private_key']);
|
|
|
161
|
- }
|
|
|
162
|
- if (!empty($data)) {
|
|
|
163
|
- $url = self::httpBuildQuery($url, $data);
|
|
|
164
|
- }
|
|
|
165
|
- /* 开启缓存的情况 */
|
|
|
166
|
- if ($cache && USE_CACHE) {
|
|
|
167
|
- // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
168
|
- $result = Cache::get($url, 'master');
|
|
|
169
|
- if (!empty($result)) {
|
|
|
170
|
- return $result;
|
|
|
171
|
- }
|
|
|
172
|
- }
|
|
|
173
|
-
|
|
|
174
|
- $ch = curl_init($url);
|
|
|
175
|
- curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
176
|
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
177
|
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
178
|
- $result = curl_exec($ch);
|
|
|
179
|
- if (!$returnJson && !empty($result)) {
|
|
|
180
|
- $result = json_decode($result, true);
|
|
|
181
|
- }
|
|
|
182
|
-
|
|
|
183
|
- curl_close($ch);
|
|
|
184
|
- $data = array();
|
|
|
185
|
-
|
|
|
186
|
- /* 开启缓存的情况 */
|
|
|
187
|
- if ($cache && USE_CACHE) {
|
|
|
188
|
- // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
189
|
- if (empty($result)) {
|
|
|
190
|
- $result = Cache::get($url, 'slave');
|
|
|
191
|
- }
|
|
|
192
|
- // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
193
|
- else {
|
|
|
194
|
- Cache::set($url, $result, $cache);
|
|
|
195
|
- }
|
|
|
196
|
- }
|
|
|
197
|
-
|
|
|
198
|
- return $result;
|
|
|
199
|
- }
|
|
|
200
|
-
|
|
|
201
|
- /**
|
|
|
202
|
- * post提交数据
|
|
|
203
|
- *
|
|
|
204
|
- * @param string $url 接口URL
|
|
|
205
|
- * @param array $data 参数列表
|
|
|
206
|
- * @param bool $returnJson 控制是否返回json格式数据
|
|
|
207
|
- * @param int $timeout 超时时间
|
|
|
208
|
- * @param array $header
|
|
|
209
|
- * @param array $cookie
|
|
|
210
|
- * @return mixed
|
|
|
211
|
- */
|
|
|
212
|
- public static function post($url, $data = array(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array())
|
|
|
213
|
- {
|
|
|
214
|
- $ch = curl_init($url);
|
|
|
215
|
-
|
|
|
216
|
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
217
|
- if (!empty($header)) {
|
|
|
218
|
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
219
|
- } else {
|
|
|
220
|
- curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
221
|
- }
|
|
|
222
|
-
|
|
|
223
|
- if (!empty($cookie)) {
|
|
|
224
|
- $cookie_str = array();
|
|
|
225
|
- foreach ($cookie as $key => $val) {
|
|
|
226
|
- $cookie_str[] = urlencode($key) . '=' . urlencode($val);
|
|
|
227
|
- }
|
|
|
228
|
- curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
|
|
|
229
|
- }
|
|
|
230
|
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 YOHOWEB');
|
|
|
231
|
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
232
|
- curl_setopt($ch, CURLOPT_POST, true);
|
|
|
233
|
- // 销毁私钥参数
|
|
|
234
|
- if (isset($data['private_key'])) {
|
|
|
235
|
- unset($data['private_key']);
|
|
|
236
|
- }
|
|
|
237
|
- if (!empty($data)) {
|
|
|
238
|
- $str = http_build_query($data, null, '&');
|
|
|
239
|
- // 新加支持application/x-www-form-urlencoded调用方式
|
|
|
240
|
- //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
241
|
- curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
|
|
|
242
|
- }
|
|
|
243
|
- $result = curl_exec($ch);
|
|
|
244
|
- if (!$returnJson && !empty($result)) {
|
|
|
245
|
- $result = json_decode($result, true);
|
|
|
246
|
- }
|
|
|
247
|
- curl_close($ch);
|
|
|
248
|
- $data = array();
|
|
|
249
|
-
|
|
|
250
|
- return $result;
|
|
|
251
|
- }
|
|
|
252
|
-
|
|
|
253
|
- /**
|
|
|
254
|
- * 批量调用接口
|
|
|
255
|
- *
|
|
|
256
|
- * @param array $urlList 接口列表
|
|
|
257
|
- * @param array $options CURL设置项
|
|
|
258
|
- * @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
259
|
- * @param int $timeout 超时时间,单位是秒
|
|
|
260
|
- * @return array
|
|
|
261
|
- */
|
|
|
262
|
- public static function getMulti($urlList = array(), $options = array(), $cache = false, $timeout = 5)
|
|
|
263
|
- {
|
|
|
264
|
- /* 开启缓存的情况 */
|
|
|
265
|
- if ($cache && USE_CACHE) {
|
|
|
266
|
- $key = md5(implode(',', array_values($urlList)));
|
|
|
267
|
- // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
268
|
- $result = Cache::get($key, 'master');
|
|
|
269
|
- if (!empty($result)) {
|
|
|
270
|
- return $result;
|
|
|
271
|
- }
|
|
|
272
|
- }
|
|
|
273
|
-
|
|
|
274
|
- $result = array();
|
|
|
275
|
- $response = array();
|
|
|
276
|
- $running = 0;
|
|
|
277
|
- $data = '';
|
|
|
278
|
- $error = '';
|
|
|
279
|
- $defaultOptions = array(
|
|
|
280
|
- CURLOPT_HEADER => 0,
|
|
|
281
|
- CURLOPT_RETURNTRANSFER => 1,
|
|
|
282
|
- CURLOPT_CONNECTTIMEOUT => $timeout,
|
|
|
283
|
- CURLOPT_TIMEOUT => $timeout,
|
|
|
284
|
- CURLOPT_NOSIGNAL => 1, //忽略所有的curl传递给php的信号,减少并发crash
|
|
|
285
|
- );
|
|
|
286
|
- $mh = curl_multi_init();
|
|
|
287
|
- $ch = array();
|
|
|
288
|
-
|
|
|
289
|
- // 应用CURL配置
|
|
|
290
|
- if (empty($options)) {
|
|
|
291
|
- $options = $defaultOptions;
|
|
|
292
|
- } else {
|
|
|
293
|
- $options = array_merge($defaultOptions, $options);
|
|
|
294
|
- }
|
|
|
295
|
-
|
|
|
296
|
- // 添加子链接句柄
|
|
|
297
|
- foreach ($urlList as $name => $api) {
|
|
|
298
|
- $ch[$name] = curl_init($api);
|
|
|
299
|
- curl_setopt_array($ch[$name], $options);
|
|
|
300
|
- curl_multi_add_handle($mh, $ch[$name]);
|
|
|
301
|
- $result[$name] = array();
|
|
|
302
|
- }
|
|
|
303
|
-
|
|
|
304
|
- // 调用API接口
|
|
|
305
|
- do {
|
|
|
306
|
- do {
|
|
|
307
|
- $status = curl_multi_exec($mh, $running);
|
|
|
308
|
- } while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
309
|
-
|
|
|
310
|
- if ($status != CURLM_OK) {
|
|
|
311
|
- break;
|
|
|
312
|
- }
|
|
|
313
|
-
|
|
|
314
|
- if ($running > 0) {
|
|
|
315
|
- curl_multi_select($mh, 0.5);
|
|
|
316
|
- }
|
|
|
317
|
- } while ($running);
|
|
|
318
|
-
|
|
|
319
|
- // 获取API接口响应的结果
|
|
|
320
|
- foreach ($urlList as $name => $api) {
|
|
|
321
|
- $error = curl_error($ch[$name]);
|
|
|
322
|
- if ($error != '') {
|
|
|
323
|
- continue;
|
|
|
324
|
- }
|
|
|
325
|
-
|
|
|
326
|
- $data = curl_multi_getcontent($ch[$name]);
|
|
|
327
|
- if (!$data) {
|
|
|
328
|
- continue;
|
|
|
329
|
- }
|
|
|
330
|
-
|
|
|
331
|
- $response = json_decode($data, true);
|
|
|
332
|
- if (empty($response['data'])) {
|
|
|
333
|
- continue;
|
|
|
334
|
- }
|
|
|
335
|
- $result[$name] = $response['data'];
|
|
|
336
|
-
|
|
|
337
|
- curl_multi_remove_handle($mh, $ch[$name]);
|
|
|
338
|
- curl_close($ch[$name]);
|
|
|
339
|
- }
|
|
|
340
|
- curl_multi_close($mh);
|
|
|
341
|
-
|
|
|
342
|
- /* 开启缓存的情况 */
|
|
|
343
|
- if ($cache && USE_CACHE) {
|
|
|
344
|
- // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
345
|
- if (empty($result[$name])) {
|
|
|
346
|
- $result = Cache::get($key, 'slave');
|
|
|
347
|
- }
|
|
|
348
|
- // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
349
|
- else {
|
|
|
350
|
- Cache::set($key, $result, $cache);
|
|
|
351
|
- }
|
|
|
352
|
- }
|
|
|
353
|
-
|
|
|
354
|
- return $result;
|
|
|
355
|
- }
|
|
|
356
|
-
|
|
|
357
|
- /**
|
|
|
358
|
- * rpc调用远程服务(YAR)
|
|
|
359
|
- *
|
|
|
360
|
- * @see http://php.net/manual/zh/yar-client.setopt.php
|
|
|
361
|
- * @param string $uri
|
|
|
362
|
- * @param string $method
|
|
|
363
|
- * @param array $parameters
|
|
|
364
|
- * @param mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
365
|
- * @param int $timeout
|
|
|
366
|
- * @return array
|
|
|
367
|
- */
|
|
|
368
|
- public static function yarClient($uri, $method, $parameters = array(), $cache = false, $timeout = 3000)
|
|
|
369
|
- {
|
|
|
370
|
- /* 开启缓存的情况 */
|
|
|
371
|
- if ($cache && USE_CACHE) {
|
|
|
372
|
- $key = self::httpBuildQuery($uri . $method, $parameters);
|
|
|
373
|
- // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
374
|
- $result = Cache::get($key, 'master');
|
|
|
375
|
- if (!empty($result)) {
|
|
|
376
|
- return $result;
|
|
|
377
|
- }
|
|
|
378
|
- }
|
|
|
379
|
-
|
|
|
380
|
- $client = new \Yar_Client($uri);
|
|
|
381
|
- $client->SetOpt(YAR_OPT_PACKAGER, 'php');
|
|
|
382
|
- $client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
|
|
|
383
|
- $client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
|
|
|
384
|
-
|
|
|
385
|
- try {
|
|
|
386
|
- $result = call_user_func_array(array($client, $method), $parameters);
|
|
|
387
|
- } catch (\Exception $e) {
|
|
|
388
|
- $result = array();
|
|
|
389
|
- }
|
|
|
390
|
-
|
|
|
391
|
- /* 开启缓存的情况 */
|
|
|
392
|
- if ($cache && USE_CACHE) {
|
|
|
393
|
- // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
394
|
- if (empty($result)) {
|
|
|
395
|
- $result = Cache::get($key, 'slave');
|
|
|
396
|
- }
|
|
|
397
|
- // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
398
|
- else {
|
|
|
399
|
- Cache::set($key, $result, $cache);
|
|
|
400
|
- }
|
|
|
401
|
- }
|
|
|
402
|
-
|
|
|
403
|
- return $result;
|
|
|
404
|
- }
|
|
|
405
|
-
|
|
|
406
|
- /**
|
|
|
407
|
- * 并行(异步)调用远程服务
|
|
|
408
|
- *
|
|
|
409
|
- * @see http://php.net/manual/zh/class.yar-concurrent-client.php
|
|
|
410
|
- * @param string $uri
|
|
|
411
|
- * @param string $method
|
|
|
412
|
- * @param array $parameter
|
|
|
413
|
- * @param callable $callback
|
|
|
414
|
- * @param int $timeout
|
|
|
415
|
- * @return void
|
|
|
416
|
- */
|
|
|
417
|
- public static function yarConcurrentCall($uri, $method, $parameters, $callback, $timeout = 3000)
|
|
|
418
|
- {
|
|
|
419
|
- \Yar_Concurrent_Client::call($uri, $method, $parameters, $callback, null, array(
|
|
|
420
|
- YAR_OPT_PACKAGER => 'php',
|
|
|
421
|
- YAR_OPT_TIMEOUT => $timeout,
|
|
|
422
|
- YAR_OPT_CONNECT_TIMEOUT => $timeout
|
|
|
423
|
- ));
|
|
|
424
|
- }
|
|
|
425
|
-
|
|
|
426
|
- public static function yarConcurrentLoop($callback = null)
|
|
|
427
|
- {
|
|
|
428
|
- \Yar_Concurrent_Client::loop($callback);
|
|
|
429
|
- }
|
|
|
430
|
-
|
|
|
431
|
- /**
|
|
|
432
|
- * 提交json格式数据请求java有关接口
|
|
|
433
|
- *
|
|
|
434
|
- * @param string $url 接口URL
|
|
|
435
|
- * @param array $data 参数列表
|
|
|
436
|
- * @param bool $returnJson 控制是否返回json格式数据
|
|
|
437
|
- * @param int $timeout 超时时间
|
|
|
438
|
- * @param array $cookie
|
|
|
439
|
- * @return mixed
|
|
|
440
|
- */
|
|
|
441
|
- public static function jsonPost($url, $data = array(), $returnJson = false, $timeout = 10, $cookie = array())
|
|
|
442
|
- {
|
|
|
443
|
- $ch = curl_init($url);
|
|
|
444
|
-
|
|
|
445
|
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
446
|
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
|
|
447
|
-
|
|
|
448
|
- if (!empty($cookie)) {
|
|
|
449
|
- $cookie_str = array();
|
|
|
450
|
- foreach ($cookie as $key => $val) {
|
|
|
451
|
- $cookie_str[] = urlencode($key) . '=' . urlencode($val);
|
|
|
452
|
- }
|
|
|
453
|
- curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
|
|
|
454
|
- }
|
|
|
455
|
-
|
|
|
456
|
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
457
|
-
|
|
|
458
|
- if (!empty($data)) {
|
|
|
459
|
- $data_string = json_encode($data);
|
|
|
460
|
-
|
|
|
461
|
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
|
|
|
462
|
- // 设置json的Header
|
|
|
463
|
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
|
464
|
- 'Content-Type: application/json',
|
|
|
465
|
- 'Content-Length: ' . strlen($data_string)
|
|
|
466
|
- ));
|
|
|
467
|
- }
|
|
|
468
|
- $result = curl_exec($ch);
|
|
|
469
|
- if (!$returnJson && !empty($result)) {
|
|
|
470
|
- $result = json_decode($result, true);
|
|
|
471
|
- }
|
|
|
472
|
- curl_close($ch);
|
|
|
473
|
- $data = array();
|
|
|
474
|
-
|
|
|
475
|
- return $result;
|
|
|
476
|
- }
|
|
|
477
|
-
|
|
|
478
|
-} |
1
|
+<?php
|
|
|
2
|
+
|
|
|
3
|
+/**
|
|
|
4
|
+ * 有货相关接口类
|
|
|
5
|
+ *
|
|
|
6
|
+ * @name Yohobuy
|
|
|
7
|
+ * @package library/Api
|
|
|
8
|
+ * @copyright yoho.inc
|
|
|
9
|
+ * @version 1.0 (2015-9-30 16:42:51)
|
|
|
10
|
+ * @author fei.hong <fei.hong@yoho.cn>
|
|
|
11
|
+ */
|
|
|
12
|
+
|
|
|
13
|
+namespace Api;
|
|
|
14
|
+
|
|
|
15
|
+use Plugin\Cache;
|
|
|
16
|
+
|
|
|
17
|
+class Yohobuy
|
|
|
18
|
+{
|
|
|
19
|
+ /* 正式环境 */
|
|
|
20
|
+
|
|
|
21
|
+// const API_URL = 'http://api2.open.yohobuy.com/';
|
|
|
22
|
+// const API_URL2 = 'http://api.open.yohobuy.com/';
|
|
|
23
|
+// const SERVICE_URL = 'http://service.api.yohobuy.com/';
|
|
|
24
|
+// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
25
|
+
|
|
|
26
|
+// const API_URL = 'http://apih5.yoho.cn/';
|
|
|
27
|
+// const API_URL2 = 'http://apih5.yoho.cn/';
|
|
|
28
|
+ // const SERVICE_URL = 'http://serviceh5.yoho.cn/';
|
|
|
29
|
+ // const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
30
|
+ // const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
31
|
+
|
|
|
32
|
+// /* 测试环境 */
|
|
|
33
|
+ // const API_URL = 'http://192.168.102.205:8080/gateway/'; // 先临时使用网关
|
|
|
34
|
+ const API_URL = 'http://testapi.yoho.cn:28078/';
|
|
|
35
|
+ const SERVICE_URL = 'http://testservice.yoho.cn:28077/';
|
|
|
36
|
+ const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
37
|
+ const API_OLD = 'http://test2.open.yohobuy.com/';
|
|
|
38
|
+
|
|
|
39
|
+ /* 预览环境 */
|
|
|
40
|
+// const API_URL = 'http://preapi.yoho.cn/';
|
|
|
41
|
+// const API_URL2 = 'http://preapi.yoho.cn/';
|
|
|
42
|
+// const SERVICE_URL = 'http://serviceh5.yoho.cn/';
|
|
|
43
|
+// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
44
|
+// const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
45
|
+
|
|
|
46
|
+ /* PC重构地址 */
|
|
|
47
|
+// const API_URL = 'http://test.open.yohobuy.com/';
|
|
|
48
|
+// const SERVICE_URL = 'http://test.service.api.yohobuy.com/';
|
|
|
49
|
+// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
50
|
+// const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
51
|
+
|
|
|
52
|
+ /**
|
|
|
53
|
+ * 私钥列表
|
|
|
54
|
+ *
|
|
|
55
|
+ * @var array
|
|
|
56
|
+ */
|
|
|
57
|
+ public static $privateKeyList = array(
|
|
|
58
|
+ 'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
|
|
|
59
|
+ 'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
|
|
|
60
|
+ 'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
|
|
|
61
|
+ 'web' => '0ed29744ed318fd28d2c07985d3ba633',
|
|
|
62
|
+ );
|
|
|
63
|
+
|
|
|
64
|
+ /**
|
|
|
65
|
+ * 取得当前的客户端类型
|
|
|
66
|
+ */
|
|
|
67
|
+ public static function clientType()
|
|
|
68
|
+ {
|
|
|
69
|
+ // 苹果设备
|
|
|
70
|
+ if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
|
|
|
71
|
+ return 'iphone';
|
|
|
72
|
+ }
|
|
|
73
|
+ // 苹果IPAD
|
|
|
74
|
+ elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
|
|
|
75
|
+
|
|
|
76
|
+ return 'ipad';
|
|
|
77
|
+ }
|
|
|
78
|
+ elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'android')) {
|
|
|
79
|
+ return 'android';
|
|
|
80
|
+ }
|
|
|
81
|
+ // 其它
|
|
|
82
|
+ else {
|
|
|
83
|
+ return 'android';
|
|
|
84
|
+ }
|
|
|
85
|
+ }
|
|
|
86
|
+
|
|
|
87
|
+ /**
|
|
|
88
|
+ * 取得当前的IP地址
|
|
|
89
|
+ *
|
|
|
90
|
+ * @param bool $int 返回int类型的ip地址,默认是
|
|
|
91
|
+ * @return mixed 当前的IP地址
|
|
|
92
|
+ */
|
|
|
93
|
+ public static function ip($int = true)
|
|
|
94
|
+ {
|
|
|
95
|
+ if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']) {
|
|
|
96
|
+ $onlineip = $_SERVER['HTTP_CLIENT_IP'];
|
|
|
97
|
+ } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
|
|
|
98
|
+ $onlineip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
99
|
+ } else {
|
|
|
100
|
+ $onlineip = $_SERVER['REMOTE_ADDR'];
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ return $int ? ip2long($onlineip) : $onlineip;
|
|
|
104
|
+ }
|
|
|
105
|
+
|
|
|
106
|
+ /**
|
|
|
107
|
+ * 取得公共的参数
|
|
|
108
|
+ *
|
|
|
109
|
+ * @return array
|
|
|
110
|
+ */
|
|
|
111
|
+ public static function param()
|
|
|
112
|
+ {
|
|
|
113
|
+ $clientType = self::clientType();
|
|
|
114
|
+ $param = array(
|
|
|
115
|
+ 'app_version' => '3.8.2',
|
|
|
116
|
+ 'client_type' => $clientType,
|
|
|
117
|
+ 'os_version' => 'yohobuy:h5',
|
|
|
118
|
+ 'private_key' => self::$privateKeyList[$clientType],
|
|
|
119
|
+ 'screen_size' => '720x1280',
|
|
|
120
|
+ 'v' => '7',
|
|
|
121
|
+ );
|
|
|
122
|
+ return $param;
|
|
|
123
|
+ }
|
|
|
124
|
+
|
|
|
125
|
+ /**
|
|
|
126
|
+ * 构建URL
|
|
|
127
|
+ *
|
|
|
128
|
+ * @param string $url
|
|
|
129
|
+ * @param array $data
|
|
|
130
|
+ * @return string
|
|
|
131
|
+ */
|
|
|
132
|
+ public static function httpBuildQuery($url, $data)
|
|
|
133
|
+ {
|
|
|
134
|
+ // 销毁私钥参数
|
|
|
135
|
+ if (isset($data['private_key'])) {
|
|
|
136
|
+ unset($data['private_key']);
|
|
|
137
|
+ }
|
|
|
138
|
+ if (strstr($url, '?') !== false) {
|
|
|
139
|
+ $url .= '&' . http_build_query($data, null, '&');
|
|
|
140
|
+ } else {
|
|
|
141
|
+ $url .= '?' . http_build_query($data, null, '&');
|
|
|
142
|
+ }
|
|
|
143
|
+ return $url;
|
|
|
144
|
+ }
|
|
|
145
|
+
|
|
|
146
|
+ /**
|
|
|
147
|
+ * get方式调用接口
|
|
|
148
|
+ *
|
|
|
149
|
+ * @param string $url 接口URL
|
|
|
150
|
+ * @param array $data 参数列表
|
|
|
151
|
+ * @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
152
|
+ * @param bool $returnJson 控制是否返回json格式数据
|
|
|
153
|
+ * @param int $timeout 超时时间
|
|
|
154
|
+ * @return mixed
|
|
|
155
|
+ */
|
|
|
156
|
+ public static function get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5)
|
|
|
157
|
+ {
|
|
|
158
|
+ // 销毁私钥参数
|
|
|
159
|
+ if (isset($data['private_key'])) {
|
|
|
160
|
+ unset($data['private_key']);
|
|
|
161
|
+ }
|
|
|
162
|
+ if (!empty($data)) {
|
|
|
163
|
+ $url = self::httpBuildQuery($url, $data);
|
|
|
164
|
+ }
|
|
|
165
|
+ /* 开启缓存的情况 */
|
|
|
166
|
+ if ($cache && USE_CACHE) {
|
|
|
167
|
+ // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
168
|
+ $result = Cache::get($url, 'master');
|
|
|
169
|
+ if (!empty($result)) {
|
|
|
170
|
+ return $result;
|
|
|
171
|
+ }
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ $ch = curl_init($url);
|
|
|
175
|
+ curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
176
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
177
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
178
|
+ $result = curl_exec($ch);
|
|
|
179
|
+ if (!$returnJson && !empty($result)) {
|
|
|
180
|
+ $result = json_decode($result, true);
|
|
|
181
|
+ }
|
|
|
182
|
+
|
|
|
183
|
+ curl_close($ch);
|
|
|
184
|
+ $data = array();
|
|
|
185
|
+
|
|
|
186
|
+ /* 开启缓存的情况 */
|
|
|
187
|
+ if ($cache && USE_CACHE) {
|
|
|
188
|
+ // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
189
|
+ if (empty($result)) {
|
|
|
190
|
+ $result = Cache::get($url, 'slave');
|
|
|
191
|
+ }
|
|
|
192
|
+ // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
193
|
+ else {
|
|
|
194
|
+ Cache::set($url, $result, $cache);
|
|
|
195
|
+ }
|
|
|
196
|
+ }
|
|
|
197
|
+
|
|
|
198
|
+ return $result;
|
|
|
199
|
+ }
|
|
|
200
|
+
|
|
|
201
|
+ /**
|
|
|
202
|
+ * post提交数据
|
|
|
203
|
+ *
|
|
|
204
|
+ * @param string $url 接口URL
|
|
|
205
|
+ * @param array $data 参数列表
|
|
|
206
|
+ * @param bool $returnJson 控制是否返回json格式数据
|
|
|
207
|
+ * @param int $timeout 超时时间
|
|
|
208
|
+ * @param array $header
|
|
|
209
|
+ * @param array $cookie
|
|
|
210
|
+ * @return mixed
|
|
|
211
|
+ */
|
|
|
212
|
+ public static function post($url, $data = array(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array())
|
|
|
213
|
+ {
|
|
|
214
|
+ $ch = curl_init($url);
|
|
|
215
|
+
|
|
|
216
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
217
|
+ if (!empty($header)) {
|
|
|
218
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
219
|
+ } else {
|
|
|
220
|
+ curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+ if (!empty($cookie)) {
|
|
|
224
|
+ $cookie_str = array();
|
|
|
225
|
+ foreach ($cookie as $key => $val) {
|
|
|
226
|
+ $cookie_str[] = urlencode($key) . '=' . urlencode($val);
|
|
|
227
|
+ }
|
|
|
228
|
+ curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
|
|
|
229
|
+ }
|
|
|
230
|
+ curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 YOHOWEB');
|
|
|
231
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
232
|
+ curl_setopt($ch, CURLOPT_POST, true);
|
|
|
233
|
+ // 销毁私钥参数
|
|
|
234
|
+ if (isset($data['private_key'])) {
|
|
|
235
|
+ unset($data['private_key']);
|
|
|
236
|
+ }
|
|
|
237
|
+ if (!empty($data)) {
|
|
|
238
|
+ $str = http_build_query($data, null, '&');
|
|
|
239
|
+ // 新加支持application/x-www-form-urlencoded调用方式
|
|
|
240
|
+ //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
241
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
|
|
|
242
|
+ }
|
|
|
243
|
+ $result = curl_exec($ch);
|
|
|
244
|
+ if (!$returnJson && !empty($result)) {
|
|
|
245
|
+ $result = json_decode($result, true);
|
|
|
246
|
+ }
|
|
|
247
|
+ curl_close($ch);
|
|
|
248
|
+ $data = array();
|
|
|
249
|
+
|
|
|
250
|
+ return $result;
|
|
|
251
|
+ }
|
|
|
252
|
+
|
|
|
253
|
+ /**
|
|
|
254
|
+ * 批量调用接口
|
|
|
255
|
+ *
|
|
|
256
|
+ * @param array $urlList 接口列表
|
|
|
257
|
+ * @param array $options CURL设置项
|
|
|
258
|
+ * @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
259
|
+ * @param int $timeout 超时时间,单位是秒
|
|
|
260
|
+ * @return array
|
|
|
261
|
+ */
|
|
|
262
|
+ public static function getMulti($urlList = array(), $options = array(), $cache = false, $timeout = 5)
|
|
|
263
|
+ {
|
|
|
264
|
+ /* 开启缓存的情况 */
|
|
|
265
|
+ if ($cache && USE_CACHE) {
|
|
|
266
|
+ $key = md5(implode(',', array_values($urlList)));
|
|
|
267
|
+ // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
268
|
+ $result = Cache::get($key, 'master');
|
|
|
269
|
+ if (!empty($result)) {
|
|
|
270
|
+ return $result;
|
|
|
271
|
+ }
|
|
|
272
|
+ }
|
|
|
273
|
+
|
|
|
274
|
+ $result = array();
|
|
|
275
|
+ $response = array();
|
|
|
276
|
+ $running = 0;
|
|
|
277
|
+ $data = '';
|
|
|
278
|
+ $error = '';
|
|
|
279
|
+ $defaultOptions = array(
|
|
|
280
|
+ CURLOPT_HEADER => 0,
|
|
|
281
|
+ CURLOPT_RETURNTRANSFER => 1,
|
|
|
282
|
+ CURLOPT_CONNECTTIMEOUT => $timeout,
|
|
|
283
|
+ CURLOPT_TIMEOUT => $timeout,
|
|
|
284
|
+ CURLOPT_NOSIGNAL => 1, //忽略所有的curl传递给php的信号,减少并发crash
|
|
|
285
|
+ );
|
|
|
286
|
+ $mh = curl_multi_init();
|
|
|
287
|
+ $ch = array();
|
|
|
288
|
+
|
|
|
289
|
+ // 应用CURL配置
|
|
|
290
|
+ if (empty($options)) {
|
|
|
291
|
+ $options = $defaultOptions;
|
|
|
292
|
+ } else {
|
|
|
293
|
+ $options = array_merge($defaultOptions, $options);
|
|
|
294
|
+ }
|
|
|
295
|
+
|
|
|
296
|
+ // 添加子链接句柄
|
|
|
297
|
+ foreach ($urlList as $name => $api) {
|
|
|
298
|
+ $ch[$name] = curl_init($api);
|
|
|
299
|
+ curl_setopt_array($ch[$name], $options);
|
|
|
300
|
+ curl_multi_add_handle($mh, $ch[$name]);
|
|
|
301
|
+ $result[$name] = array();
|
|
|
302
|
+ }
|
|
|
303
|
+
|
|
|
304
|
+ // 调用API接口
|
|
|
305
|
+ do {
|
|
|
306
|
+ do {
|
|
|
307
|
+ $status = curl_multi_exec($mh, $running);
|
|
|
308
|
+ } while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
309
|
+
|
|
|
310
|
+ if ($status != CURLM_OK) {
|
|
|
311
|
+ break;
|
|
|
312
|
+ }
|
|
|
313
|
+
|
|
|
314
|
+ if ($running > 0) {
|
|
|
315
|
+ curl_multi_select($mh, 0.5);
|
|
|
316
|
+ }
|
|
|
317
|
+ } while ($running);
|
|
|
318
|
+
|
|
|
319
|
+ // 获取API接口响应的结果
|
|
|
320
|
+ foreach ($urlList as $name => $api) {
|
|
|
321
|
+ $error = curl_error($ch[$name]);
|
|
|
322
|
+ if ($error != '') {
|
|
|
323
|
+ continue;
|
|
|
324
|
+ }
|
|
|
325
|
+
|
|
|
326
|
+ $data = curl_multi_getcontent($ch[$name]);
|
|
|
327
|
+ if (!$data) {
|
|
|
328
|
+ continue;
|
|
|
329
|
+ }
|
|
|
330
|
+
|
|
|
331
|
+ $response = json_decode($data, true);
|
|
|
332
|
+ if (empty($response['data'])) {
|
|
|
333
|
+ continue;
|
|
|
334
|
+ }
|
|
|
335
|
+ $result[$name] = $response['data'];
|
|
|
336
|
+
|
|
|
337
|
+ curl_multi_remove_handle($mh, $ch[$name]);
|
|
|
338
|
+ curl_close($ch[$name]);
|
|
|
339
|
+ }
|
|
|
340
|
+ curl_multi_close($mh);
|
|
|
341
|
+
|
|
|
342
|
+ /* 开启缓存的情况 */
|
|
|
343
|
+ if ($cache && USE_CACHE) {
|
|
|
344
|
+ // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
345
|
+ if (empty($result[$name])) {
|
|
|
346
|
+ $result = Cache::get($key, 'slave');
|
|
|
347
|
+ }
|
|
|
348
|
+ // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
349
|
+ else {
|
|
|
350
|
+ Cache::set($key, $result, $cache);
|
|
|
351
|
+ }
|
|
|
352
|
+ }
|
|
|
353
|
+
|
|
|
354
|
+ return $result;
|
|
|
355
|
+ }
|
|
|
356
|
+
|
|
|
357
|
+ /**
|
|
|
358
|
+ * rpc调用远程服务(YAR)
|
|
|
359
|
+ *
|
|
|
360
|
+ * @see http://php.net/manual/zh/yar-client.setopt.php
|
|
|
361
|
+ * @param string $uri
|
|
|
362
|
+ * @param string $method
|
|
|
363
|
+ * @param array $parameters
|
|
|
364
|
+ * @param mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
365
|
+ * @param int $timeout
|
|
|
366
|
+ * @return array
|
|
|
367
|
+ */
|
|
|
368
|
+ public static function yarClient($uri, $method, $parameters = array(), $cache = false, $timeout = 3000)
|
|
|
369
|
+ {
|
|
|
370
|
+ /* 开启缓存的情况 */
|
|
|
371
|
+ if ($cache && USE_CACHE) {
|
|
|
372
|
+ $key = self::httpBuildQuery($uri . $method, $parameters);
|
|
|
373
|
+ // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
374
|
+ $result = Cache::get($key, 'master');
|
|
|
375
|
+ if (!empty($result)) {
|
|
|
376
|
+ return $result;
|
|
|
377
|
+ }
|
|
|
378
|
+ }
|
|
|
379
|
+
|
|
|
380
|
+ $client = new \Yar_Client($uri);
|
|
|
381
|
+ $client->SetOpt(YAR_OPT_PACKAGER, 'php');
|
|
|
382
|
+ $client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
|
|
|
383
|
+ $client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
|
|
|
384
|
+
|
|
|
385
|
+ try {
|
|
|
386
|
+ $result = call_user_func_array(array($client, $method), $parameters);
|
|
|
387
|
+ } catch (\Exception $e) {
|
|
|
388
|
+ $result = array();
|
|
|
389
|
+ }
|
|
|
390
|
+
|
|
|
391
|
+ /* 开启缓存的情况 */
|
|
|
392
|
+ if ($cache && USE_CACHE) {
|
|
|
393
|
+ // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
394
|
+ if (empty($result)) {
|
|
|
395
|
+ $result = Cache::get($key, 'slave');
|
|
|
396
|
+ }
|
|
|
397
|
+ // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
398
|
+ else {
|
|
|
399
|
+ Cache::set($key, $result, $cache);
|
|
|
400
|
+ }
|
|
|
401
|
+ }
|
|
|
402
|
+
|
|
|
403
|
+ return $result;
|
|
|
404
|
+ }
|
|
|
405
|
+
|
|
|
406
|
+ /**
|
|
|
407
|
+ * 并行(异步)调用远程服务
|
|
|
408
|
+ *
|
|
|
409
|
+ * @see http://php.net/manual/zh/class.yar-concurrent-client.php
|
|
|
410
|
+ * @param string $uri
|
|
|
411
|
+ * @param string $method
|
|
|
412
|
+ * @param array $parameter
|
|
|
413
|
+ * @param callable $callback
|
|
|
414
|
+ * @param int $timeout
|
|
|
415
|
+ * @return void
|
|
|
416
|
+ */
|
|
|
417
|
+ public static function yarConcurrentCall($uri, $method, $parameters, $callback, $timeout = 3000)
|
|
|
418
|
+ {
|
|
|
419
|
+ \Yar_Concurrent_Client::call($uri, $method, $parameters, $callback, null, array(
|
|
|
420
|
+ YAR_OPT_PACKAGER => 'php',
|
|
|
421
|
+ YAR_OPT_TIMEOUT => $timeout,
|
|
|
422
|
+ YAR_OPT_CONNECT_TIMEOUT => $timeout
|
|
|
423
|
+ ));
|
|
|
424
|
+ }
|
|
|
425
|
+
|
|
|
426
|
+ public static function yarConcurrentLoop($callback = null)
|
|
|
427
|
+ {
|
|
|
428
|
+ \Yar_Concurrent_Client::loop($callback);
|
|
|
429
|
+ }
|
|
|
430
|
+
|
|
|
431
|
+ /**
|
|
|
432
|
+ * 提交json格式数据请求java有关接口
|
|
|
433
|
+ *
|
|
|
434
|
+ * @param string $url 接口URL
|
|
|
435
|
+ * @param array $data 参数列表
|
|
|
436
|
+ * @param bool $returnJson 控制是否返回json格式数据
|
|
|
437
|
+ * @param int $timeout 超时时间
|
|
|
438
|
+ * @param array $cookie
|
|
|
439
|
+ * @return mixed
|
|
|
440
|
+ */
|
|
|
441
|
+ public static function jsonPost($url, $data = array(), $returnJson = false, $timeout = 10, $cookie = array())
|
|
|
442
|
+ {
|
|
|
443
|
+ $ch = curl_init($url);
|
|
|
444
|
+
|
|
|
445
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
446
|
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
|
|
447
|
+
|
|
|
448
|
+ if (!empty($cookie)) {
|
|
|
449
|
+ $cookie_str = array();
|
|
|
450
|
+ foreach ($cookie as $key => $val) {
|
|
|
451
|
+ $cookie_str[] = urlencode($key) . '=' . urlencode($val);
|
|
|
452
|
+ }
|
|
|
453
|
+ curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
|
|
|
454
|
+ }
|
|
|
455
|
+
|
|
|
456
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
457
|
+
|
|
|
458
|
+ if (!empty($data)) {
|
|
|
459
|
+ $data_string = json_encode($data);
|
|
|
460
|
+
|
|
|
461
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
|
|
|
462
|
+ // 设置json的Header
|
|
|
463
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
|
464
|
+ 'Content-Type: application/json',
|
|
|
465
|
+ 'Content-Length: ' . strlen($data_string)
|
|
|
466
|
+ ));
|
|
|
467
|
+ }
|
|
|
468
|
+ $result = curl_exec($ch);
|
|
|
469
|
+ if (!$returnJson && !empty($result)) {
|
|
|
470
|
+ $result = json_decode($result, true);
|
|
|
471
|
+ }
|
|
|
472
|
+ curl_close($ch);
|
|
|
473
|
+ $data = array();
|
|
|
474
|
+
|
|
|
475
|
+ return $result;
|
|
|
476
|
+ }
|
|
|
477
|
+
|
|
|
478
|
+} |