Yohobuy.php
26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
<?php
/**
* 有货相关接口类
*
* @name Yohobuy
* @package library/Api
* @copyright yoho.inc
* @version 1.0 (2015-9-30 16:42:51)
* @author fei.hong <fei.hong@yoho.cn>
*/
namespace Api;
use WebPlugin\Cache;
use WebPlugin\UdpLog;
class Yohobuy
{
/**
* 登录老接口Api配置
*/
const API_LOGIN_URL = 'http://api.open.yohobuy.com';
const API_LOGIN_URL_TEST = 'http://test.open.yohobuy.com';
/**
* 私钥列表
*
* @var array
*/
public static $privateKeyList = array(
'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
'web' => '0ed29744ed318fd28d2c07985d3ba633',
'h5' => 'fd4ad5fcfa0de589ef238c0e7331b585',
);
//分流服务器接口
public static $interfaceShunt = array(
'tencentServers' => array(
'api' => array('123.206.1.98', '123.206.2.80'),
'service' => array('123.206.1.98', '123.206.2.80')
),
'awsServers' => array(
'api' => 'app-java-168863769.cn-north-1.elb.amazonaws.com.cn',
'service' => 'service-yoho-579825100.cn-north-1.elb.amazonaws.com.cn'
)
);
/**
* 取得当前的客户端类型
*/
public static function clientType()
{
if(self::isMobile()) {
return 'h5';
}
else {
return 'web';
}
// // 苹果设备
// if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
// return 'iphone';
// }
// // 苹果IPAD
// elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
// return 'ipad';
// }
// elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'android')) {
// return 'android';
// }
// // 其它
// else {
// return 'android';
// }
}
/**
* 取得当前的IP地址
*
* @param bool $int 返回int类型的ip地址,默认是
* @return mixed 当前的IP地址
*/
public static function ip($int = true)
{
if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']) {
$onlineip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
$onlineip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
return $int ? ip2long($onlineip) : $onlineip;
}
/**
* 取得公共的参数
*
* @return array
*/
public static function param()
{
$clientType = self::clientType();
$param = array(
'app_version' => '3.8.2',
'client_type' => $clientType,
'os_version' => 'yohobuy:h5',
'private_key' => self::$privateKeyList[$clientType],
'screen_size' => '720x1280',
'v' => '7',
);
return $param;
}
/**
* 构建URL
*
* @param string $url
* @param array $data
* @return string
*/
public static function httpBuildQuery($url, $data)
{
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (strstr($url, '?') !== false) {
$url .= '&' . http_build_query($data, null, '&');
} else {
$url .= '?' . http_build_query($data, null, '&');
}
return $url;
}
/**
* get方式调用接口
*
* @param string $url 接口URL
* @param array $data 参数列表
* @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @param null $userAgent
* @param bool $useGzip 是否启用Gzip
* @return mixed
*/
public static function get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5, $userAgent = null, $useGzip = true)
{
$isApi = false;
$isService = false;
$urlBack = $url;
if (USE_INTER_FACE_SHUNT) {
if (strpos($url, 'api')) {
$isApi = true;
$url = self::_getUrl($url, $data);
} else if (strpos($url, 'service')) {
$isService = true;
$url = self::_getUrl($url, $data);
}
//$url 返回为空,走原先的url
if (empty($url)) {
$isApi = false;
$isService = false;
$url = $urlBack;
}
}
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$url = self::httpBuildQuery($url, $data);
} //echo $url;
UdpLog::debug('get shut api', $url);
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($url, 'master');
if (!empty($result)) {
return $result;
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($isApi) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: api.yoho.cn'));
} else if ($isService) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: service.yoho.cn'));
}
if ($useGzip) {
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
}
if (!empty($userAgent)) {
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
//如果分流执行失败,则走原来的逻辑
if (USE_INTER_FACE_SHUNT && $httpCode !== 200) {
curl_close($ch);
return self::_get($urlBack, $data, $cache, $returnJson, $timeout, $userAgent, $useGzip);
}
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($url, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($url, $result, $cache);
}
}
return $result;
}
private static function _get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5, $userAgent = null, $useGzip = true)
{
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$url = self::httpBuildQuery($url, $data);
} //echo $url;
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($url, 'master');
if (!empty($result)) {
return $result;
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($useGzip) {
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
}
if (!empty($userAgent)) {
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}
UdpLog::debug('call api: ', $url);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($url, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($url, $result, $cache);
}
}
return $result;
}
/**
* post提交数据
*
* @param string $url 接口URL
* @param array $data 参数列表
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @param array $header
* @param array $cookie
* @return mixed
*/
public static function post($url, $data = array(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array())
{
$isApi = false;
$isService = false;
$urlBack = $url;
if (USE_INTER_FACE_SHUNT) {
if (strpos($url, 'api')) {
$isApi = true;
$url = self::_getUrl($url, $data);
} else if (strpos($url, 'service')) {
$isService = true;
$url = self::_getUrl($url, $data);
}
//$url 返回为空,走原先的url
if (empty($url)) {
$isApi = false;
$isService = false;
$url = $urlBack;
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($isApi) {
array_push($header, 'Host: api.yoho.cn');
} else if ($isService) {
array_push($header, 'Host: service.yoho.cn');
}
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
curl_setopt($ch, CURLOPT_HEADER, 0);
}
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 YOHOWEB');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$str = http_build_query($data, null, '&');
// 新加支持application/x-www-form-urlencoded调用方式
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
}
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
UdpLog::debug('post api', array('url' => $url, 'httpCode' => $httpCode));
//如果分流执行失败,则走原来的逻辑
if (USE_INTER_FACE_SHUNT && $httpCode !== 200) {
curl_close($ch);
return self::_post($urlBack, $data, $returnJson, $timeout, $header, $cookie);
}
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
return $result;
}
public static function _post($url, $data = array(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
} else {
curl_setopt($ch, CURLOPT_HEADER, 0);
}
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 YOHOWEB');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$str = http_build_query($data, null, '&');
// 新加支持application/x-www-form-urlencoded调用方式
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
}
UdpLog::debug('post fail api', $url);
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
return $result;
}
/**
* 批量调用接口
*
* @param array $urlList 接口列表
* @param array $options CURL设置项
* @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param int $timeout 超时时间,单位是秒
* @param boolean $isOriginalData 是否原始数据返回 (默认false, 只返回data数据)
* @return array
*/
public static function getMulti($urlList = array(), $options = array(), $cache = false, $timeout = 5, $isOriginalData = false)
{
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
$key = md5(implode(',', array_values($urlList)));
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$result = array();
$response = array();
$running = 0;
$data = '';
$error = '';
$defaultOptions = array(
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_NOSIGNAL => 1, //忽略所有的curl传递给php的信号,减少并发crash
);
$mh = curl_multi_init();
$ch = array();
// 应用CURL配置
if (empty($options)) {
$options = $defaultOptions;
} else {
$options = array_merge($defaultOptions, $options);
}
// 添加子链接句柄
foreach ($urlList as $name => $api) {
$ch[$name] = curl_init($api);
curl_setopt_array($ch[$name], $options);
curl_multi_add_handle($mh, $ch[$name]);
$result[$name] = array();
}
// 调用API接口
do {
do {
$status = curl_multi_exec($mh, $running);
} while ($status == CURLM_CALL_MULTI_PERFORM);
if ($status != CURLM_OK) {
break;
}
if ($running > 0) {
curl_multi_select($mh, 0.5);
}
} while ($running);
// 获取API接口响应的结果
foreach ($urlList as $name => $api) {
$error = curl_error($ch[$name]);
if ($error != '') {
continue;
}
$data = curl_multi_getcontent($ch[$name]);
if (!$data) {
continue;
}
$response = json_decode($data, true);
if(!$isOriginalData) {//只返回data数据
if (!isset($response['data'])) {
continue;
}
$result[$name] = $response['data'];
}
else {
$result[$name] = $response;
}
curl_multi_remove_handle($mh, $ch[$name]);
curl_close($ch[$name]);
}
curl_multi_close($mh);
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result[$name])) {
$result = Cache::get($key, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($key, $result, $cache);
}
}
return $result;
}
/**
* rpc调用远程服务(YAR)
*
* @see http://php.net/manual/zh/yar-client.setopt.php
* @param string $uri
* @param string $method
* @param array $parameters
* @param mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
* @param int $timeout
* @return array
*/
public static function yarClient($uri, $method, $parameters = array(), $cache = false, $timeout = 3000)
{
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
$key = self::httpBuildQuery($uri . $method, $parameters);
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
$client = new \Yar_Client($uri);
$client->SetOpt(YAR_OPT_PACKAGER, 'php');
$client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
try {
$result = call_user_func_array(array($client, $method), $parameters);
} catch (\Exception $e) {
$result = array();
}
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($key, $result, $cache);
}
}
return $result;
}
/**
* 并行(异步)调用远程服务
*
* @see http://php.net/manual/zh/class.yar-concurrent-client.php
* @param string $uri
* @param string $method
* @param array $parameter
* @param callable $callback
* @param int $timeout
* @return void
*/
public static function yarConcurrentCall($uri, $method, $parameters, $callback, $timeout = 3000)
{
\Yar_Concurrent_Client::call($uri, $method, $parameters, $callback, null, array(
YAR_OPT_PACKAGER => 'php',
YAR_OPT_TIMEOUT => $timeout,
YAR_OPT_CONNECT_TIMEOUT => $timeout
));
}
public static function yarConcurrentLoop($callback = null)
{
\Yar_Concurrent_Client::loop($callback);
}
/**
* 提交json格式数据请求java有关接口
*
* @param string $url 接口URL
* @param array $data 参数列表
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @param array $cookie
* @return mixed
*/
public static function jsonPost($url, $data = array(), $returnJson = false, $timeout = 10, $cookie = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
if (!empty($cookie)) {
$cookie_str = array();
foreach ($cookie as $key => $val) {
$cookie_str[] = urlencode($key) . '=' . urlencode($val);
}
curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($data)) {
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
// 设置json的Header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
}
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
return $result;
}
/**
* 获取用户User-Agent
* @return bool
*/
public static function getUserAgent()
{
if (!isset($_SERVER['HTTP_USER_AGENT']) || empty($_SERVER['HTTP_USER_AGENT'])) {
return '';
}
return $_SERVER['HTTP_USER_AGENT'];
}
/**
* 判断是否手机
* @return bool
*/
public static function isMobile()
{
$userAgent = self::getUserAgent();
if (stristr($userAgent, 'ipad')) {
return false;
}
$mobileAgents = array('iphone','android',"240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
$isMobile = false;
foreach ($mobileAgents as $device) {
if (stristr($userAgent, $device)) {
$isMobile = true;
break;
}
}
return $isMobile;
}
/**
*根据配置决定请求Java接口的地址
*/
private static function _getUrl($url, $param)
{
if (USE_CACHE === false || USE_INTER_FACE_SHUNT === false) {
return false;
}
UdpLog::debug('read memcache');
$interfaceShunt = Cache::getBy('INTERFACE_SHUT_KEY', 'master');
UdpLog::debug('read memcahe: ', $interfaceShunt);
if (empty($interfaceShunt)) {
return false;
}
$interfaceShunt = json_decode($interfaceShunt, true);
if (empty($interfaceShunt['cloud'])) {
return false;
}
$uid = isset($param['uid']) ? intval($param['uid']) : 0;
$mod = 0;
$cloud = intval($interfaceShunt['cloud']);
$ips = '';
switch ($cloud) {
case 1:
$ips = $interfaceShunt['aws'];
break;
case 2:
$ips = $interfaceShunt['tencent'];
break;
case 3:
if (empty($uid) || empty($interfaceShunt['strategy']) ||
empty($interfaceShunt['strategy']['model']) ||
empty($interfaceShunt['strategy']['tencentMax'])) {
break;
} else {
$ips = $uid % $interfaceShunt['strategy']['model'] > $interfaceShunt['strategy']['tencentMax'] ?
$interfaceShunt['aws'] : $interfaceShunt['tencent'];
}
break;
default:
$ips = '';
break;
}
//未登录情况下
if (empty($uid) && empty($ips) && !empty($interfaceShunt['strategy']) &&
!empty($interfaceShunt['strategy']['notLogin'])) {
$ips = $interfaceShunt[$interfaceShunt['strategy']['notLogin']];
}
if (empty($ips)) {
return false;
}
$paseUrl = parse_url($url);
if (strpos($url, 'api')) {
if (empty($ips['api.yoho.cn'])) {
return false;
}
$num = array_rand($ips['api.yoho.cn']);
if (empty($ips['api.yoho.cn'][$num]['ip'])) {
return false;
}
$url = 'http://' . $ips['api.yoho.cn'][$num]['ip'] . rtrim($paseUrl['path'], '/') . '/';
} else if (strpos($url, 'service')) {
if (empty($ips['service.yoho.cn'])) {
return false;
}
$num = array_rand($ips['service.yoho.cn']);
if (empty($ips['service.yoho.cn'][$num]['ip'])) {
return false;
}
$url = 'http://' . $ips['service.yoho.cn'][$num]['ip'] . rtrim($paseUrl['path'], '/') . '/';
}
return $url;
}
}