|
|
1
|
+<<<<<<< Updated upstream
|
1
|
<?php
|
2
|
<?php
|
2
|
|
3
|
|
3
|
/**
|
4
|
/**
|
|
@@ -465,3 +466,483 @@ class Yohobuy |
|
@@ -465,3 +466,483 @@ class Yohobuy |
465
|
}
|
466
|
}
|
466
|
|
467
|
|
467
|
}
|
468
|
}
|
|
|
469
|
+=======
|
|
|
470
|
+<?php
|
|
|
471
|
+
|
|
|
472
|
+/**
|
|
|
473
|
+ * 有货相关接口类
|
|
|
474
|
+ *
|
|
|
475
|
+ * @name Yohobuy
|
|
|
476
|
+ * @package library/Api
|
|
|
477
|
+ * @copyright yoho.inc
|
|
|
478
|
+ * @version 1.0 (2015-9-30 16:42:51)
|
|
|
479
|
+ * @author fei.hong <fei.hong@yoho.cn>
|
|
|
480
|
+ */
|
|
|
481
|
+
|
|
|
482
|
+namespace Api;
|
|
|
483
|
+
|
|
|
484
|
+use Plugin\Cache;
|
|
|
485
|
+
|
|
|
486
|
+class Yohobuy
|
|
|
487
|
+{
|
|
|
488
|
+ /* 正式环境 */
|
|
|
489
|
+
|
|
|
490
|
+// const API_URL = 'http://api2.open.yohobuy.com/';
|
|
|
491
|
+// const API_URL2 = 'http://api.open.yohobuy.com/';
|
|
|
492
|
+// const SERVICE_URL = 'http://service.api.yohobuy.com/';
|
|
|
493
|
+// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
494
|
+
|
|
|
495
|
+ const API_URL = 'http://apih5.yoho.cn/';
|
|
|
496
|
+ const API_URL2 = 'http://apih5.yoho.cn/';
|
|
|
497
|
+ const SERVICE_URL = 'http://serviceh5.yoho.cn/';
|
|
|
498
|
+ const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
499
|
+ const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
500
|
+
|
|
|
501
|
+// /* 测试环境 */
|
|
|
502
|
+ // const API_URL = 'http://192.168.102.205:8080/gateway/'; // 先临时使用网关
|
|
|
503
|
+// const API_URL = 'http://testapi.yoho.cn:28078/';
|
|
|
504
|
+// const SERVICE_URL = 'http://testservice.yoho.cn:28077/';
|
|
|
505
|
+// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
506
|
+// const API_OLD = 'http://test2.open.yohobuy.com/';
|
|
|
507
|
+
|
|
|
508
|
+ /* 预览环境 */
|
|
|
509
|
+// const API_URL = 'http://preapi.yoho.cn/';
|
|
|
510
|
+// const API_URL2 = 'http://preapi.yoho.cn/';
|
|
|
511
|
+// const SERVICE_URL = 'http://serviceh5.yoho.cn/';
|
|
|
512
|
+// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
513
|
+// const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
514
|
+
|
|
|
515
|
+ /* PC重构地址 */
|
|
|
516
|
+// const API_URL = 'http://test.open.yohobuy.com/';
|
|
|
517
|
+// const SERVICE_URL = 'http://test.service.api.yohobuy.com/';
|
|
|
518
|
+// const YOHOBUY_URL = 'http://www.yohobuy.com/';
|
|
|
519
|
+// const API_OLD = 'http://api2.open.yohobuy.com/';
|
|
|
520
|
+
|
|
|
521
|
+ /**
|
|
|
522
|
+ * 私钥列表
|
|
|
523
|
+ *
|
|
|
524
|
+ * @var array
|
|
|
525
|
+ */
|
|
|
526
|
+ public static $privateKeyList = array(
|
|
|
527
|
+ 'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
|
|
|
528
|
+ 'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
|
|
|
529
|
+ 'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
|
|
|
530
|
+ 'web' => '0ed29744ed318fd28d2c07985d3ba633',
|
|
|
531
|
+ );
|
|
|
532
|
+
|
|
|
533
|
+ /**
|
|
|
534
|
+ * 取得当前的客户端类型
|
|
|
535
|
+ */
|
|
|
536
|
+ public static function clientType()
|
|
|
537
|
+ {
|
|
|
538
|
+ // 苹果设备
|
|
|
539
|
+ if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
|
|
|
540
|
+ return 'iphone';
|
|
|
541
|
+ }
|
|
|
542
|
+ // 苹果IPAD
|
|
|
543
|
+ elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
|
|
|
544
|
+
|
|
|
545
|
+ return 'ipad';
|
|
|
546
|
+ }
|
|
|
547
|
+ elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'android')) {
|
|
|
548
|
+ return 'android';
|
|
|
549
|
+ }
|
|
|
550
|
+ // 其它
|
|
|
551
|
+ else {
|
|
|
552
|
+ return 'android';
|
|
|
553
|
+ }
|
|
|
554
|
+ }
|
|
|
555
|
+
|
|
|
556
|
+ /**
|
|
|
557
|
+ * 取得当前的IP地址
|
|
|
558
|
+ *
|
|
|
559
|
+ * @param bool $int 返回int类型的ip地址,默认是
|
|
|
560
|
+ * @return mixed 当前的IP地址
|
|
|
561
|
+ */
|
|
|
562
|
+ public static function ip($int = true)
|
|
|
563
|
+ {
|
|
|
564
|
+ if (isset($_SERVER['HTTP_CLIENT_IP']) && $_SERVER['HTTP_CLIENT_IP']) {
|
|
|
565
|
+ $onlineip = $_SERVER['HTTP_CLIENT_IP'];
|
|
|
566
|
+ } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
|
|
|
567
|
+ $onlineip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
568
|
+ } else {
|
|
|
569
|
+ $onlineip = $_SERVER['REMOTE_ADDR'];
|
|
|
570
|
+ }
|
|
|
571
|
+
|
|
|
572
|
+ return $int ? ip2long($onlineip) : $onlineip;
|
|
|
573
|
+ }
|
|
|
574
|
+
|
|
|
575
|
+ /**
|
|
|
576
|
+ * 取得公共的参数
|
|
|
577
|
+ *
|
|
|
578
|
+ * @return array
|
|
|
579
|
+ */
|
|
|
580
|
+ public static function param()
|
|
|
581
|
+ {
|
|
|
582
|
+ $clientType = self::clientType();
|
|
|
583
|
+ $param = array(
|
|
|
584
|
+ 'app_version' => '3.8.2',
|
|
|
585
|
+ 'client_type' => $clientType,
|
|
|
586
|
+ 'os_version' => 'yohobuy:h5',
|
|
|
587
|
+ 'private_key' => self::$privateKeyList[$clientType],
|
|
|
588
|
+ 'screen_size' => '720x1280',
|
|
|
589
|
+ 'v' => '7',
|
|
|
590
|
+ );
|
|
|
591
|
+ return $param;
|
|
|
592
|
+ }
|
|
|
593
|
+
|
|
|
594
|
+ /**
|
|
|
595
|
+ * 构建URL
|
|
|
596
|
+ *
|
|
|
597
|
+ * @param string $url
|
|
|
598
|
+ * @param array $data
|
|
|
599
|
+ * @return string
|
|
|
600
|
+ */
|
|
|
601
|
+ public static function httpBuildQuery($url, $data)
|
|
|
602
|
+ {
|
|
|
603
|
+ // 销毁私钥参数
|
|
|
604
|
+ if (isset($data['private_key'])) {
|
|
|
605
|
+ unset($data['private_key']);
|
|
|
606
|
+ }
|
|
|
607
|
+ if (strstr($url, '?') !== false) {
|
|
|
608
|
+ $url .= '&' . http_build_query($data, null, '&');
|
|
|
609
|
+ } else {
|
|
|
610
|
+ $url .= '?' . http_build_query($data, null, '&');
|
|
|
611
|
+ }
|
|
|
612
|
+ return $url;
|
|
|
613
|
+ }
|
|
|
614
|
+
|
|
|
615
|
+ /**
|
|
|
616
|
+ * get方式调用接口
|
|
|
617
|
+ *
|
|
|
618
|
+ * @param string $url 接口URL
|
|
|
619
|
+ * @param array $data 参数列表
|
|
|
620
|
+ * @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
621
|
+ * @param bool $returnJson 控制是否返回json格式数据
|
|
|
622
|
+ * @param int $timeout 超时时间
|
|
|
623
|
+ * @return mixed
|
|
|
624
|
+ */
|
|
|
625
|
+ public static function get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5)
|
|
|
626
|
+ {
|
|
|
627
|
+ // 销毁私钥参数
|
|
|
628
|
+ if (isset($data['private_key'])) {
|
|
|
629
|
+ unset($data['private_key']);
|
|
|
630
|
+ }
|
|
|
631
|
+ if (!empty($data)) {
|
|
|
632
|
+ $url = self::httpBuildQuery($url, $data);
|
|
|
633
|
+ }
|
|
|
634
|
+ /* 开启缓存的情况 */
|
|
|
635
|
+ if ($cache && USE_CACHE) {
|
|
|
636
|
+ // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
637
|
+ $result = Cache::get($url, 'master');
|
|
|
638
|
+ if (!empty($result)) {
|
|
|
639
|
+ return $result;
|
|
|
640
|
+ }
|
|
|
641
|
+ }
|
|
|
642
|
+
|
|
|
643
|
+ $ch = curl_init($url);
|
|
|
644
|
+ curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
645
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
646
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
647
|
+ $result = curl_exec($ch);
|
|
|
648
|
+ if (!$returnJson && !empty($result)) {
|
|
|
649
|
+ $result = json_decode($result, true);
|
|
|
650
|
+ }
|
|
|
651
|
+
|
|
|
652
|
+ curl_close($ch);
|
|
|
653
|
+ $data = array();
|
|
|
654
|
+
|
|
|
655
|
+ /* 开启缓存的情况 */
|
|
|
656
|
+ if ($cache && USE_CACHE) {
|
|
|
657
|
+ // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
658
|
+ if (empty($result)) {
|
|
|
659
|
+ $result = Cache::get($url, 'slave');
|
|
|
660
|
+ }
|
|
|
661
|
+ // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
662
|
+ else {
|
|
|
663
|
+ Cache::set($url, $result, $cache);
|
|
|
664
|
+ }
|
|
|
665
|
+ }
|
|
|
666
|
+
|
|
|
667
|
+ return $result;
|
|
|
668
|
+ }
|
|
|
669
|
+
|
|
|
670
|
+ /**
|
|
|
671
|
+ * post提交数据
|
|
|
672
|
+ *
|
|
|
673
|
+ * @param string $url 接口URL
|
|
|
674
|
+ * @param array $data 参数列表
|
|
|
675
|
+ * @param bool $returnJson 控制是否返回json格式数据
|
|
|
676
|
+ * @param int $timeout 超时时间
|
|
|
677
|
+ * @param array $header
|
|
|
678
|
+ * @param array $cookie
|
|
|
679
|
+ * @return mixed
|
|
|
680
|
+ */
|
|
|
681
|
+ public static function post($url, $data = array(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array())
|
|
|
682
|
+ {
|
|
|
683
|
+ $ch = curl_init($url);
|
|
|
684
|
+
|
|
|
685
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
686
|
+ if (!empty($header)) {
|
|
|
687
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
688
|
+ } else {
|
|
|
689
|
+ curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
690
|
+ }
|
|
|
691
|
+
|
|
|
692
|
+ if (!empty($cookie)) {
|
|
|
693
|
+ $cookie_str = array();
|
|
|
694
|
+ foreach ($cookie as $key => $val) {
|
|
|
695
|
+ $cookie_str[] = urlencode($key) . '=' . urlencode($val);
|
|
|
696
|
+ }
|
|
|
697
|
+ curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
|
|
|
698
|
+ }
|
|
|
699
|
+ curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 YOHOWEB');
|
|
|
700
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
701
|
+ curl_setopt($ch, CURLOPT_POST, true);
|
|
|
702
|
+ // 销毁私钥参数
|
|
|
703
|
+ if (isset($data['private_key'])) {
|
|
|
704
|
+ unset($data['private_key']);
|
|
|
705
|
+ }
|
|
|
706
|
+ if (!empty($data)) {
|
|
|
707
|
+ $str = http_build_query($data, null, '&');
|
|
|
708
|
+ // 新加支持application/x-www-form-urlencoded调用方式
|
|
|
709
|
+ //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
710
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
|
|
|
711
|
+ }
|
|
|
712
|
+ $result = curl_exec($ch);
|
|
|
713
|
+ if (!$returnJson && !empty($result)) {
|
|
|
714
|
+ $result = json_decode($result, true);
|
|
|
715
|
+ }
|
|
|
716
|
+ curl_close($ch);
|
|
|
717
|
+ $data = array();
|
|
|
718
|
+
|
|
|
719
|
+ return $result;
|
|
|
720
|
+ }
|
|
|
721
|
+
|
|
|
722
|
+ /**
|
|
|
723
|
+ * 批量调用接口
|
|
|
724
|
+ *
|
|
|
725
|
+ * @param array $urlList 接口列表
|
|
|
726
|
+ * @param array $options CURL设置项
|
|
|
727
|
+ * @parma mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
728
|
+ * @param int $timeout 超时时间,单位是秒
|
|
|
729
|
+ * @return array
|
|
|
730
|
+ */
|
|
|
731
|
+ public static function getMulti($urlList = array(), $options = array(), $cache = false, $timeout = 5)
|
|
|
732
|
+ {
|
|
|
733
|
+ /* 开启缓存的情况 */
|
|
|
734
|
+ if ($cache && USE_CACHE) {
|
|
|
735
|
+ $key = md5(implode(',', array_values($urlList)));
|
|
|
736
|
+ // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
737
|
+ $result = Cache::get($key, 'master');
|
|
|
738
|
+ if (!empty($result)) {
|
|
|
739
|
+ return $result;
|
|
|
740
|
+ }
|
|
|
741
|
+ }
|
|
|
742
|
+
|
|
|
743
|
+ $result = array();
|
|
|
744
|
+ $response = array();
|
|
|
745
|
+ $running = 0;
|
|
|
746
|
+ $data = '';
|
|
|
747
|
+ $error = '';
|
|
|
748
|
+ $defaultOptions = array(
|
|
|
749
|
+ CURLOPT_HEADER => 0,
|
|
|
750
|
+ CURLOPT_RETURNTRANSFER => 1,
|
|
|
751
|
+ CURLOPT_CONNECTTIMEOUT => $timeout,
|
|
|
752
|
+ CURLOPT_TIMEOUT => $timeout,
|
|
|
753
|
+ CURLOPT_NOSIGNAL => 1, //忽略所有的curl传递给php的信号,减少并发crash
|
|
|
754
|
+ );
|
|
|
755
|
+ $mh = curl_multi_init();
|
|
|
756
|
+ $ch = array();
|
|
|
757
|
+
|
|
|
758
|
+ // 应用CURL配置
|
|
|
759
|
+ if (empty($options)) {
|
|
|
760
|
+ $options = $defaultOptions;
|
|
|
761
|
+ } else {
|
|
|
762
|
+ $options = array_merge($defaultOptions, $options);
|
|
|
763
|
+ }
|
|
|
764
|
+
|
|
|
765
|
+ // 添加子链接句柄
|
|
|
766
|
+ foreach ($urlList as $name => $api) {
|
|
|
767
|
+ $ch[$name] = curl_init($api);
|
|
|
768
|
+ curl_setopt_array($ch[$name], $options);
|
|
|
769
|
+ curl_multi_add_handle($mh, $ch[$name]);
|
|
|
770
|
+ $result[$name] = array();
|
|
|
771
|
+ }
|
|
|
772
|
+
|
|
|
773
|
+ // 调用API接口
|
|
|
774
|
+ do {
|
|
|
775
|
+ do {
|
|
|
776
|
+ $status = curl_multi_exec($mh, $running);
|
|
|
777
|
+ } while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
778
|
+
|
|
|
779
|
+ if ($status != CURLM_OK) {
|
|
|
780
|
+ break;
|
|
|
781
|
+ }
|
|
|
782
|
+
|
|
|
783
|
+ if ($running > 0) {
|
|
|
784
|
+ curl_multi_select($mh, 0.5);
|
|
|
785
|
+ }
|
|
|
786
|
+ } while ($running);
|
|
|
787
|
+
|
|
|
788
|
+ // 获取API接口响应的结果
|
|
|
789
|
+ foreach ($urlList as $name => $api) {
|
|
|
790
|
+ $error = curl_error($ch[$name]);
|
|
|
791
|
+ if ($error != '') {
|
|
|
792
|
+ continue;
|
|
|
793
|
+ }
|
|
|
794
|
+
|
|
|
795
|
+ $data = curl_multi_getcontent($ch[$name]);
|
|
|
796
|
+ if (!$data) {
|
|
|
797
|
+ continue;
|
|
|
798
|
+ }
|
|
|
799
|
+
|
|
|
800
|
+ $response = json_decode($data, true);
|
|
|
801
|
+ if (empty($response['data'])) {
|
|
|
802
|
+ continue;
|
|
|
803
|
+ }
|
|
|
804
|
+ $result[$name] = $response['data'];
|
|
|
805
|
+
|
|
|
806
|
+ curl_multi_remove_handle($mh, $ch[$name]);
|
|
|
807
|
+ curl_close($ch[$name]);
|
|
|
808
|
+ }
|
|
|
809
|
+ curl_multi_close($mh);
|
|
|
810
|
+
|
|
|
811
|
+ /* 开启缓存的情况 */
|
|
|
812
|
+ if ($cache && USE_CACHE) {
|
|
|
813
|
+ // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
814
|
+ if (empty($result[$name])) {
|
|
|
815
|
+ $result = Cache::get($key, 'slave');
|
|
|
816
|
+ }
|
|
|
817
|
+ // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
818
|
+ else {
|
|
|
819
|
+ Cache::set($key, $result, $cache);
|
|
|
820
|
+ }
|
|
|
821
|
+ }
|
|
|
822
|
+
|
|
|
823
|
+ return $result;
|
|
|
824
|
+ }
|
|
|
825
|
+
|
|
|
826
|
+ /**
|
|
|
827
|
+ * rpc调用远程服务(YAR)
|
|
|
828
|
+ *
|
|
|
829
|
+ * @see http://php.net/manual/zh/yar-client.setopt.php
|
|
|
830
|
+ * @param string $uri
|
|
|
831
|
+ * @param string $method
|
|
|
832
|
+ * @param array $parameters
|
|
|
833
|
+ * @param mixed $cache 控制是否启用接口数据的缓存(时间单位为秒). 如3600表示缓存1小时, false表示不缓存
|
|
|
834
|
+ * @param int $timeout
|
|
|
835
|
+ * @return array
|
|
|
836
|
+ */
|
|
|
837
|
+ public static function yarClient($uri, $method, $parameters = array(), $cache = false, $timeout = 3000)
|
|
|
838
|
+ {
|
|
|
839
|
+ /* 开启缓存的情况 */
|
|
|
840
|
+ if ($cache && USE_CACHE) {
|
|
|
841
|
+ $key = self::httpBuildQuery($uri . $method, $parameters);
|
|
|
842
|
+ // 先尝试获取一级缓存(master), 有数据则直接返回.
|
|
|
843
|
+ $result = Cache::get($key, 'master');
|
|
|
844
|
+ if (!empty($result)) {
|
|
|
845
|
+ return $result;
|
|
|
846
|
+ }
|
|
|
847
|
+ }
|
|
|
848
|
+
|
|
|
849
|
+ $client = new \Yar_Client($uri);
|
|
|
850
|
+ $client->SetOpt(YAR_OPT_PACKAGER, 'php');
|
|
|
851
|
+ $client->SetOpt(YAR_OPT_TIMEOUT, $timeout);
|
|
|
852
|
+ $client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, $timeout);
|
|
|
853
|
+
|
|
|
854
|
+ try {
|
|
|
855
|
+ $result = call_user_func_array(array($client, $method), $parameters);
|
|
|
856
|
+ } catch (\Exception $e) {
|
|
|
857
|
+ $result = array();
|
|
|
858
|
+ }
|
|
|
859
|
+
|
|
|
860
|
+ /* 开启缓存的情况 */
|
|
|
861
|
+ if ($cache && USE_CACHE) {
|
|
|
862
|
+ // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
|
|
|
863
|
+ if (empty($result)) {
|
|
|
864
|
+ $result = Cache::get($key, 'slave');
|
|
|
865
|
+ }
|
|
|
866
|
+ // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
|
|
|
867
|
+ else {
|
|
|
868
|
+ Cache::set($key, $result, $cache);
|
|
|
869
|
+ }
|
|
|
870
|
+ }
|
|
|
871
|
+
|
|
|
872
|
+ return $result;
|
|
|
873
|
+ }
|
|
|
874
|
+
|
|
|
875
|
+ /**
|
|
|
876
|
+ * 并行(异步)调用远程服务
|
|
|
877
|
+ *
|
|
|
878
|
+ * @see http://php.net/manual/zh/class.yar-concurrent-client.php
|
|
|
879
|
+ * @param string $uri
|
|
|
880
|
+ * @param string $method
|
|
|
881
|
+ * @param array $parameter
|
|
|
882
|
+ * @param callable $callback
|
|
|
883
|
+ * @param int $timeout
|
|
|
884
|
+ * @return void
|
|
|
885
|
+ */
|
|
|
886
|
+ public static function yarConcurrentCall($uri, $method, $parameters, $callback, $timeout = 3000)
|
|
|
887
|
+ {
|
|
|
888
|
+ \Yar_Concurrent_Client::call($uri, $method, $parameters, $callback, null, array(
|
|
|
889
|
+ YAR_OPT_PACKAGER => 'php',
|
|
|
890
|
+ YAR_OPT_TIMEOUT => $timeout,
|
|
|
891
|
+ YAR_OPT_CONNECT_TIMEOUT => $timeout
|
|
|
892
|
+ ));
|
|
|
893
|
+ }
|
|
|
894
|
+
|
|
|
895
|
+ public static function yarConcurrentLoop($callback = null)
|
|
|
896
|
+ {
|
|
|
897
|
+ \Yar_Concurrent_Client::loop($callback);
|
|
|
898
|
+ }
|
|
|
899
|
+
|
|
|
900
|
+ /**
|
|
|
901
|
+ * 提交json格式数据请求java有关接口
|
|
|
902
|
+ *
|
|
|
903
|
+ * @param string $url 接口URL
|
|
|
904
|
+ * @param array $data 参数列表
|
|
|
905
|
+ * @param bool $returnJson 控制是否返回json格式数据
|
|
|
906
|
+ * @param int $timeout 超时时间
|
|
|
907
|
+ * @param array $cookie
|
|
|
908
|
+ * @return mixed
|
|
|
909
|
+ */
|
|
|
910
|
+ public static function jsonPost($url, $data = array(), $returnJson = false, $timeout = 10, $cookie = array())
|
|
|
911
|
+ {
|
|
|
912
|
+ $ch = curl_init($url);
|
|
|
913
|
+
|
|
|
914
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
915
|
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
|
|
916
|
+
|
|
|
917
|
+ if (!empty($cookie)) {
|
|
|
918
|
+ $cookie_str = array();
|
|
|
919
|
+ foreach ($cookie as $key => $val) {
|
|
|
920
|
+ $cookie_str[] = urlencode($key) . '=' . urlencode($val);
|
|
|
921
|
+ }
|
|
|
922
|
+ curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
|
|
|
923
|
+ }
|
|
|
924
|
+
|
|
|
925
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
926
|
+
|
|
|
927
|
+ if (!empty($data)) {
|
|
|
928
|
+ $data_string = json_encode($data);
|
|
|
929
|
+
|
|
|
930
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
|
|
|
931
|
+ // 设置json的Header
|
|
|
932
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
|
933
|
+ 'Content-Type: application/json',
|
|
|
934
|
+ 'Content-Length: ' . strlen($data_string)
|
|
|
935
|
+ ));
|
|
|
936
|
+ }
|
|
|
937
|
+ $result = curl_exec($ch);
|
|
|
938
|
+ if (!$returnJson && !empty($result)) {
|
|
|
939
|
+ $result = json_decode($result, true);
|
|
|
940
|
+ }
|
|
|
941
|
+ curl_close($ch);
|
|
|
942
|
+ $data = array();
|
|
|
943
|
+
|
|
|
944
|
+ return $result;
|
|
|
945
|
+ }
|
|
|
946
|
+
|
|
|
947
|
+}
|
|
|
948
|
+>>>>>>> Stashed changes |