Yohobuy.php
9.16 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
<?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 Plugin\Cache;
class Yohobuy
{
const API_URL = 'http://api2.open.yohobuy.com/';
const SERVICE_URL = 'http://service.api.yohobuy.com/';
const YOHOBUY_URL = 'http://www.yohobuy.com/';
/**
* 私钥列表
*
* @var array
*/
private static $privateKeyList = array(
'android' => 'fd4ad5fcfa0de589ef238c0e7331b585',
'iphone' => 'a85bb0674e08986c6b115d5e3a4884fa',
'ipad' => 'ad9fcda2e679cf9229e37feae2cdcf80',
);
/**
* 取得当前的客户端类型
*/
public static function clientType()
{
// 苹果设备
if (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
return 'iphone';
}
// 苹果IPAD
elseif (strstr($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
return 'ipad';
}
// 其它
else {
return 'android';
}
}
/**
* 取得公共的参数
*
* @return array
*/
public static function param()
{
$clientType = self::clientType();
$param = array(
'app_version' => '3.6',
'client_type' => $clientType,
'os_version' => 'yohobuy:h5',
'private_key' => self::$privateKeyList[$clientType],
'screen_size' => '720x1280',
'v' => '6',
);
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 参数列表
* @param bool $returnJson 控制是否返回json格式数据
* @param int $timeout 超时时间
* @return mixed
*/
public static function get($url, $data = array(), $returnJson = false, $timeout = 5)
{
// // 代表是否开启缓存
// $useCache = $cache && isset($data['client_secret']);
//
// /* 先尝试获取一级缓存(master), 有数据则直接返回 */
// if ($useCache) {
// $key = md5($url . $data['client_secret']);
// $result = Cache::get($key, 'master');
// if (!empty($result)) {
// return $result;
// }
// }
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$url = self::httpBuildQuery($url, $data);
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
// /* 设置一级二级缓存 或 获取二级缓存(slave) */
// if ($useCache) {
// // 如果接口异常没数据返回,则获取二级缓存
// if (empty($result)) {
// $result = Cache::get($key, 'slave');
// }
// // 如果接口正常有数据返回,则设置数据缓存
// else {
// Cache::set($key, $result);
// }
// }
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 = 5, $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_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$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 超时时间,单位是秒
* @return array
*/
public static function getMulti($urlList = array(), $options = array(), $cache = false, $timeout = 3)
{
$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 (empty($response['data'])) {
continue;
}
$result[$name] = $response['data'];
curl_multi_remove_handle($mh, $ch[$name]);
curl_close($ch[$name]);
}
curl_multi_close($mh);
return $result;
}
/**
* rpc调用远程服务(YAR)
*
* @see http://php.net/manual/zh/yar-client.setopt.php
* @return array
*/
public static function yarClient($uri, $method, $parameters = array(), $timeout = 3000)
{
$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();
}
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);
}
}