Showing
1 changed file
with
124 additions
and
6 deletions
@@ -180,14 +180,16 @@ class Yohobuy | @@ -180,14 +180,16 @@ class Yohobuy | ||
180 | { | 180 | { |
181 | $isApi = false; | 181 | $isApi = false; |
182 | $isService = false; | 182 | $isService = false; |
183 | + $urlBack = $url; | ||
183 | 184 | ||
184 | if (USE_INTER_FACE_SHUNT) { | 185 | if (USE_INTER_FACE_SHUNT) { |
185 | if (strpos($url, 'api.')) { | 186 | if (strpos($url, 'api.')) { |
186 | $isApi = true; | 187 | $isApi = true; |
188 | + $url = self::_getUrl($url, $data); | ||
187 | } else if (strpos($url, 'service.')) { | 189 | } else if (strpos($url, 'service.')) { |
188 | $isService = true; | 190 | $isService = true; |
191 | + $url = self::_getUrl($url, $data); | ||
189 | } | 192 | } |
190 | - $url = self::_getUrl($url, $data); | ||
191 | } | 193 | } |
192 | 194 | ||
193 | // 销毁私钥参数 | 195 | // 销毁私钥参数 |
@@ -229,6 +231,14 @@ class Yohobuy | @@ -229,6 +231,14 @@ class Yohobuy | ||
229 | } | 231 | } |
230 | 232 | ||
231 | $result = curl_exec($ch); | 233 | $result = curl_exec($ch); |
234 | + $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE); | ||
235 | + | ||
236 | + //如果分流执行失败,则走原来的逻辑 | ||
237 | + if (USE_INTER_FACE_SHUNT && $httpCode !== 200) { | ||
238 | + curl_close($ch); | ||
239 | + return self::_get($urlBack, $data, $cache, $returnJson, $timeout, $userAgent, $useGzip); | ||
240 | + } | ||
241 | + | ||
232 | if (!$returnJson && !empty($result)) { | 242 | if (!$returnJson && !empty($result)) { |
233 | $result = json_decode($result, true); | 243 | $result = json_decode($result, true); |
234 | } | 244 | } |
@@ -251,6 +261,63 @@ class Yohobuy | @@ -251,6 +261,63 @@ class Yohobuy | ||
251 | return $result; | 261 | return $result; |
252 | } | 262 | } |
253 | 263 | ||
264 | + private static function _get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5, $userAgent = null, $useGzip = true) | ||
265 | + { | ||
266 | + // 销毁私钥参数 | ||
267 | + if (isset($data['private_key'])) { | ||
268 | + unset($data['private_key']); | ||
269 | + } | ||
270 | + | ||
271 | + if (!empty($data)) { | ||
272 | + $url = self::httpBuildQuery($url, $data); | ||
273 | + } //echo $url; | ||
274 | + | ||
275 | + /* 开启缓存的情况 */ | ||
276 | + if ($cache && USE_CACHE) { | ||
277 | + // 先尝试获取一级缓存(master), 有数据则直接返回. | ||
278 | + $result = Cache::get($url, 'master'); | ||
279 | + if (!empty($result)) { | ||
280 | + return $result; | ||
281 | + } | ||
282 | + } | ||
283 | + | ||
284 | + | ||
285 | + $ch = curl_init($url); | ||
286 | + curl_setopt($ch, CURLOPT_HEADER, 0); | ||
287 | + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | ||
288 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
289 | + | ||
290 | + if ($useGzip) { | ||
291 | + curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); | ||
292 | + } | ||
293 | + | ||
294 | + if (!empty($userAgent)) { | ||
295 | + curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); | ||
296 | + } | ||
297 | + | ||
298 | + $result = curl_exec($ch); | ||
299 | + $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE); | ||
300 | + if (!$returnJson && !empty($result)) { | ||
301 | + $result = json_decode($result, true); | ||
302 | + } | ||
303 | + | ||
304 | + curl_close($ch); | ||
305 | + $data = array(); | ||
306 | + | ||
307 | + /* 开启缓存的情况 */ | ||
308 | + if ($cache && USE_CACHE) { | ||
309 | + // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据. | ||
310 | + if (empty($result)) { | ||
311 | + $result = Cache::get($url, 'slave'); | ||
312 | + } | ||
313 | + // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据. | ||
314 | + else { | ||
315 | + Cache::set($url, $result, $cache); | ||
316 | + } | ||
317 | + } | ||
318 | + | ||
319 | + return $result; | ||
320 | + } | ||
254 | /** | 321 | /** |
255 | * post提交数据 | 322 | * post提交数据 |
256 | * | 323 | * |
@@ -266,14 +333,16 @@ class Yohobuy | @@ -266,14 +333,16 @@ class Yohobuy | ||
266 | { | 333 | { |
267 | $isApi = false; | 334 | $isApi = false; |
268 | $isService = false; | 335 | $isService = false; |
336 | + $urlBack = $url; | ||
269 | 337 | ||
270 | if (USE_INTER_FACE_SHUNT) { | 338 | if (USE_INTER_FACE_SHUNT) { |
271 | if (strpos($url, 'api.')) { | 339 | if (strpos($url, 'api.')) { |
272 | $isApi = true; | 340 | $isApi = true; |
341 | + $url = self::_getUrl($url, $data); | ||
273 | } else if (strpos($url, 'service.')) { | 342 | } else if (strpos($url, 'service.')) { |
274 | $isService = true; | 343 | $isService = true; |
344 | + $url = self::_getUrl($url, $data); | ||
275 | } | 345 | } |
276 | - $url = self::_getUrl($url, $data); | ||
277 | } | 346 | } |
278 | 347 | ||
279 | $ch = curl_init($url); | 348 | $ch = curl_init($url); |
@@ -312,6 +381,55 @@ class Yohobuy | @@ -312,6 +381,55 @@ class Yohobuy | ||
312 | curl_setopt($ch, CURLOPT_POSTFIELDS, $str); | 381 | curl_setopt($ch, CURLOPT_POSTFIELDS, $str); |
313 | } | 382 | } |
314 | $result = curl_exec($ch); | 383 | $result = curl_exec($ch); |
384 | + | ||
385 | + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
386 | + //如果分流执行失败,则走原来的逻辑 | ||
387 | + if (USE_INTER_FACE_SHUNT && $httpCode !== 200) { | ||
388 | + curl_close($ch); | ||
389 | + return self::_post($urlBack, $data, $returnJson, $timeout, $header, $cookie); | ||
390 | + } | ||
391 | + | ||
392 | + if (!$returnJson && !empty($result)) { | ||
393 | + $result = json_decode($result, true); | ||
394 | + } | ||
395 | + curl_close($ch); | ||
396 | + $data = array(); | ||
397 | + | ||
398 | + return $result; | ||
399 | + } | ||
400 | + | ||
401 | + public static function _post($url, $data = array(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array()) | ||
402 | + { | ||
403 | + $ch = curl_init($url); | ||
404 | + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | ||
405 | + | ||
406 | + if (!empty($header)) { | ||
407 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $header); | ||
408 | + } else { | ||
409 | + curl_setopt($ch, CURLOPT_HEADER, 0); | ||
410 | + } | ||
411 | + if (!empty($cookie)) { | ||
412 | + $cookie_str = array(); | ||
413 | + foreach ($cookie as $key => $val) { | ||
414 | + $cookie_str[] = urlencode($key) . '=' . urlencode($val); | ||
415 | + } | ||
416 | + curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str)); | ||
417 | + } | ||
418 | + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 YOHOWEB'); | ||
419 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
420 | + curl_setopt($ch, CURLOPT_POST, true); | ||
421 | + | ||
422 | + // 销毁私钥参数 | ||
423 | + if (isset($data['private_key'])) { | ||
424 | + unset($data['private_key']); | ||
425 | + } | ||
426 | + if (!empty($data)) { | ||
427 | + $str = http_build_query($data, null, '&'); | ||
428 | + // 新加支持application/x-www-form-urlencoded调用方式 | ||
429 | + //curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | ||
430 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $str); | ||
431 | + } | ||
432 | + $result = curl_exec($ch); | ||
315 | if (!$returnJson && !empty($result)) { | 433 | if (!$returnJson && !empty($result)) { |
316 | $result = json_decode($result, true); | 434 | $result = json_decode($result, true); |
317 | } | 435 | } |
@@ -606,18 +724,18 @@ class Yohobuy | @@ -606,18 +724,18 @@ class Yohobuy | ||
606 | if ($mod > 128 || $uid === 0) { | 724 | if ($mod > 128 || $uid === 0) { |
607 | $paseUrl = parse_url($url); | 725 | $paseUrl = parse_url($url); |
608 | if (strpos($url, 'api.')) { | 726 | if (strpos($url, 'api.')) { |
609 | - $url = 'http://' . self::$interfaceShunt['awsServers']['api'] . trim($paseUrl['path'], '/') . '/'; | 727 | + $url = 'http://' . self::$interfaceShunt['awsServers']['api'] . rtrim($paseUrl['path'], '/') . '/'; |
610 | } else if (strpos($url, 'service.')) { | 728 | } else if (strpos($url, 'service.')) { |
611 | - $url = 'http://' . self::$interfaceShunt['awsServers']['service'] . trim($paseUrl['path'], '/') . '/'; | 729 | + $url = 'http://' . self::$interfaceShunt['awsServers']['service'] . rtrim($paseUrl['path'], '/') . '/'; |
612 | } | 730 | } |
613 | } else { | 731 | } else { |
614 | $paseUrl = parse_url($url); | 732 | $paseUrl = parse_url($url); |
615 | if (strpos($url, 'api.')) { | 733 | if (strpos($url, 'api.')) { |
616 | $num = array_rand(self::$interfaceShunt['tencentServers']['api']); | 734 | $num = array_rand(self::$interfaceShunt['tencentServers']['api']); |
617 | - $url = 'http://' . self::$interfaceShunt['tencentServers']['api'][$num] . trim($paseUrl['path'], '/') . '/'; | 735 | + $url = 'http://' . self::$interfaceShunt['tencentServers']['api'][$num] . rtrim($paseUrl['path'], '/') . '/'; |
618 | } else if (strpos($url, 'service.')) { | 736 | } else if (strpos($url, 'service.')) { |
619 | $num = array_rand(self::$interfaceShunt['tencentServers']['service']); | 737 | $num = array_rand(self::$interfaceShunt['tencentServers']['service']); |
620 | - $url = 'http://' . self::$interfaceShunt['tencentServers']['service'][$num] . trim($paseUrl['path'], '/') . '/'; | 738 | + $url = 'http://' . self::$interfaceShunt['tencentServers']['service'][$num] . rtrim($paseUrl['path'], '/') . '/'; |
621 | } | 739 | } |
622 | } | 740 | } |
623 | 741 |
-
Please register or login to post a comment