|
|
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
|
+namespace Api;
|
|
|
13
|
+
|
|
|
14
|
+//use Api\Sign;
|
|
|
15
|
+
|
|
|
16
|
+class Yohobuy
|
|
|
17
|
+{
|
|
|
18
|
+ const API_URL = 'http://api.open.yohobuy.com/';
|
|
|
19
|
+ const SERVICE_URL = 'http://service.api.yohobuy.com/';
|
|
|
20
|
+
|
|
|
21
|
+ /**
|
|
|
22
|
+ * 私钥列表
|
|
|
23
|
+ *
|
|
|
24
|
+ * @var array
|
|
|
25
|
+ */
|
|
|
26
|
+ public static $privateKeyList = array(
|
|
|
27
|
+ 'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
|
|
|
28
|
+ 'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
|
|
|
29
|
+ 'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
|
|
|
30
|
+ );
|
|
|
31
|
+
|
|
|
32
|
+ /**
|
|
|
33
|
+ * 取得公共的参数
|
|
|
34
|
+ *
|
|
|
35
|
+ * @return array
|
|
|
36
|
+ */
|
|
|
37
|
+ public static function param()
|
|
|
38
|
+ {
|
|
|
39
|
+ $param = array(
|
|
|
40
|
+ 'client_type' => '',
|
|
|
41
|
+ 'app_version' => '',
|
|
|
42
|
+ 'os_version' => '',
|
|
|
43
|
+ 'screen_size' => '',
|
|
|
44
|
+ 'v' => '',
|
|
|
45
|
+ );
|
|
|
46
|
+ return $param;
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ /**
|
|
|
50
|
+ * get方式调用接口
|
|
|
51
|
+ *
|
|
|
52
|
+ * @param string $url 接口URL
|
|
|
53
|
+ * @param int $timeout 超时时间
|
|
|
54
|
+ * @return mixed
|
|
|
55
|
+ */
|
|
|
56
|
+ public static function get($url, $timeout = 5)
|
|
|
57
|
+ {
|
|
|
58
|
+ $ch = curl_init($url);
|
|
|
59
|
+ curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
60
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
61
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
62
|
+ $result = curl_exec($ch);
|
|
|
63
|
+ if (!empty($result)) {
|
|
|
64
|
+ $result = json_decode($result, true);
|
|
|
65
|
+ }
|
|
|
66
|
+ curl_close($ch);
|
|
|
67
|
+
|
|
|
68
|
+ return $result;
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+
|
|
|
72
|
+ /**
|
|
|
73
|
+ * post提交数据
|
|
|
74
|
+ *
|
|
|
75
|
+ * @param string $url 接口URL
|
|
|
76
|
+ * @param array $data
|
|
|
77
|
+ * @param int $timeout
|
|
|
78
|
+ * @param array $header
|
|
|
79
|
+ * @param array $cookie
|
|
|
80
|
+ * @return mixed
|
|
|
81
|
+ */
|
|
|
82
|
+ public static function post($url, $data = array(), $timeout = 5, $header = array(), $cookie = array())
|
|
|
83
|
+ {
|
|
|
84
|
+ $ch = curl_init($url);
|
|
|
85
|
+ curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
86
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
87
|
+
|
|
|
88
|
+ if (!empty($header)) {
|
|
|
89
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ if (!empty($cookie)) {
|
|
|
93
|
+ $cookie_str = array();
|
|
|
94
|
+ foreach ($cookie as $key => $val) {
|
|
|
95
|
+ $cookie_str[] = urlencode($key) . '=' . urlencode($val);
|
|
|
96
|
+ }
|
|
|
97
|
+ curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
|
|
|
98
|
+ }
|
|
|
99
|
+
|
|
|
100
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
101
|
+ curl_setopt($ch, CURLOPT_POST, true);
|
|
|
102
|
+ if (!empty($data)) {
|
|
|
103
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
104
|
+ }
|
|
|
105
|
+ $result = curl_exec($ch);
|
|
|
106
|
+ if (!empty($result)) {
|
|
|
107
|
+ $result = json_decode($result, true);
|
|
|
108
|
+ }
|
|
|
109
|
+ curl_close($ch);
|
|
|
110
|
+
|
|
|
111
|
+ return $result;
|
|
|
112
|
+ }
|
|
|
113
|
+
|
|
|
114
|
+ /**
|
|
|
115
|
+ * 批量调用接口
|
|
|
116
|
+ *
|
|
|
117
|
+ * @param array $urlList 接口列表
|
|
|
118
|
+ * @param array $options CURL设置项
|
|
|
119
|
+ * @return array
|
|
|
120
|
+ */
|
|
|
121
|
+ public function loop($urlList = array(), $options = array())
|
|
|
122
|
+ {
|
|
|
123
|
+ $result = array();
|
|
|
124
|
+ $response = array();
|
|
|
125
|
+ $running = null;
|
|
|
126
|
+ $data = '';
|
|
|
127
|
+ $error = '';
|
|
|
128
|
+ $defaultOptions = array(
|
|
|
129
|
+ CURLOPT_HEADER => 0,
|
|
|
130
|
+ CURLOPT_RETURNTRANSFER => 1,
|
|
|
131
|
+ CURLOPT_CONNECTTIMEOUT => 5,
|
|
|
132
|
+ CURLOPT_TIMEOUT => 5,
|
|
|
133
|
+ );
|
|
|
134
|
+ $mh = curl_multi_init();
|
|
|
135
|
+ $ch = array();
|
|
|
136
|
+
|
|
|
137
|
+ // 应用CURL配置
|
|
|
138
|
+ if (empty($options)) {
|
|
|
139
|
+ $options = $defaultOptions;
|
|
|
140
|
+ } else {
|
|
|
141
|
+ $options = array_merge($defaultOptions, $options);
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ // 添加子链接句柄
|
|
|
145
|
+ foreach ($urlList as $name => $api) {
|
|
|
146
|
+ $ch[$name] = curl_init($api);
|
|
|
147
|
+ curl_setopt_array($ch[$name], $options);
|
|
|
148
|
+ curl_multi_add_handle($mh, $ch[$name]);
|
|
|
149
|
+ $result[$name] = array();
|
|
|
150
|
+ }
|
|
|
151
|
+
|
|
|
152
|
+ // 调用API接口
|
|
|
153
|
+ do {
|
|
|
154
|
+ $status = curl_multi_exec($mh, $running);
|
|
|
155
|
+ }
|
|
|
156
|
+ while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
157
|
+ while ($running && $status == CURLM_OK) {
|
|
|
158
|
+ if (curl_multi_select($mh, 0.5) != -1) {
|
|
|
159
|
+ do {
|
|
|
160
|
+ $status = curl_multi_exec($mh, $running);
|
|
|
161
|
+ }
|
|
|
162
|
+ while ($status == CURLM_CALL_MULTI_PERFORM);
|
|
|
163
|
+ }
|
|
|
164
|
+ }
|
|
|
165
|
+
|
|
|
166
|
+ // 获取API接口响应的结果
|
|
|
167
|
+ foreach ($urlList as $name => $api) {
|
|
|
168
|
+ $error = curl_error($ch[$name]);
|
|
|
169
|
+ if ($error != '') {
|
|
|
170
|
+ continue;
|
|
|
171
|
+ }
|
|
|
172
|
+
|
|
|
173
|
+ $data = curl_multi_getcontent($ch[$name]);
|
|
|
174
|
+ if (!$data) {
|
|
|
175
|
+ continue;
|
|
|
176
|
+ }
|
|
|
177
|
+
|
|
|
178
|
+ $response = json_decode($data, true);
|
|
|
179
|
+ if (empty($response['data'])) {
|
|
|
180
|
+ continue;
|
|
|
181
|
+ }
|
|
|
182
|
+ $result[$name] = $response['data'];
|
|
|
183
|
+
|
|
|
184
|
+ curl_multi_remove_handle($mh, $ch[$name]);
|
|
|
185
|
+ curl_close($ch[$name]);
|
|
|
186
|
+ }
|
|
|
187
|
+ curl_multi_close($mh);
|
|
|
188
|
+
|
|
|
189
|
+ return $data;
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+} |