Authored by 梁志锋

Merge remote-tracking branch 'remotes/origin/feature/serverUrl'

@@ -150,14 +150,16 @@ class Yohobuy @@ -150,14 +150,16 @@ class Yohobuy
150 { 150 {
151 $isApi = false; 151 $isApi = false;
152 $isService = false; 152 $isService = false;
  153 + $urlBack = $url;
153 154
154 if (USE_INTER_FACE_SHUNT) { 155 if (USE_INTER_FACE_SHUNT) {
155 if (strpos($url, 'api.')) { 156 if (strpos($url, 'api.')) {
156 $isApi = true; 157 $isApi = true;
  158 + $url = self::_getUrl($url, $data);
157 } else if (strpos($url, 'service.')) { 159 } else if (strpos($url, 'service.')) {
158 $isService = true; 160 $isService = true;
  161 + $url = self::_getUrl($url, $data);
159 } 162 }
160 - $url = self::_getUrl($url, $data);  
161 } 163 }
162 164
163 // 销毁私钥参数 165 // 销毁私钥参数
@@ -199,6 +201,14 @@ class Yohobuy @@ -199,6 +201,14 @@ class Yohobuy
199 } 201 }
200 202
201 $result = curl_exec($ch); 203 $result = curl_exec($ch);
  204 + $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  205 +
  206 + //如果分流执行失败,则走原来的逻辑
  207 + if (USE_INTER_FACE_SHUNT && $httpCode !== 200) {
  208 + curl_close($ch);
  209 + return self::_get($urlBack, $data, $cache, $returnJson, $timeout, $userAgent, $useGzip);
  210 + }
  211 +
202 if (!$returnJson && !empty($result)) { 212 if (!$returnJson && !empty($result)) {
203 $result = json_decode($result, true); 213 $result = json_decode($result, true);
204 } 214 }
@@ -221,6 +231,63 @@ class Yohobuy @@ -221,6 +231,63 @@ class Yohobuy
221 return $result; 231 return $result;
222 } 232 }
223 233
  234 + private static function _get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5, $userAgent = null, $useGzip = true)
  235 + {
  236 + // 销毁私钥参数
  237 + if (isset($data['private_key'])) {
  238 + unset($data['private_key']);
  239 + }
  240 +
  241 + if (!empty($data)) {
  242 + $url = self::httpBuildQuery($url, $data);
  243 + } //echo $url;
  244 +
  245 + /* 开启缓存的情况 */
  246 + if ($cache && USE_CACHE) {
  247 + // 先尝试获取一级缓存(master), 有数据则直接返回.
  248 + $result = Cache::get($url, 'master');
  249 + if (!empty($result)) {
  250 + return $result;
  251 + }
  252 + }
  253 +
  254 +
  255 + $ch = curl_init($url);
  256 + curl_setopt($ch, CURLOPT_HEADER, 0);
  257 + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  258 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  259 +
  260 + if ($useGzip) {
  261 + curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  262 + }
  263 +
  264 + if (!empty($userAgent)) {
  265 + curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  266 + }
  267 +
  268 + $result = curl_exec($ch);
  269 + $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  270 + if (!$returnJson && !empty($result)) {
  271 + $result = json_decode($result, true);
  272 + }
  273 +
  274 + curl_close($ch);
  275 + $data = array();
  276 +
  277 + /* 开启缓存的情况 */
  278 + if ($cache && USE_CACHE) {
  279 + // 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
  280 + if (empty($result)) {
  281 + $result = Cache::get($url, 'slave');
  282 + }
  283 + // 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
  284 + else {
  285 + Cache::set($url, $result, $cache);
  286 + }
  287 + }
  288 +
  289 + return $result;
  290 + }
224 /** 291 /**
225 * post提交数据 292 * post提交数据
226 * 293 *
@@ -236,14 +303,16 @@ class Yohobuy @@ -236,14 +303,16 @@ class Yohobuy
236 { 303 {
237 $isApi = false; 304 $isApi = false;
238 $isService = false; 305 $isService = false;
  306 + $urlBack = $url;
239 307
240 if (USE_INTER_FACE_SHUNT) { 308 if (USE_INTER_FACE_SHUNT) {
241 if (strpos($url, 'api.')) { 309 if (strpos($url, 'api.')) {
242 $isApi = true; 310 $isApi = true;
  311 + $url = self::_getUrl($url, $data);
243 } else if (strpos($url, 'service.')) { 312 } else if (strpos($url, 'service.')) {
244 $isService = true; 313 $isService = true;
  314 + $url = self::_getUrl($url, $data);
245 } 315 }
246 - $url = self::_getUrl($url, $data);  
247 } 316 }
248 317
249 $ch = curl_init($url); 318 $ch = curl_init($url);
@@ -282,6 +351,55 @@ class Yohobuy @@ -282,6 +351,55 @@ class Yohobuy
282 curl_setopt($ch, CURLOPT_POSTFIELDS, $str); 351 curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
283 } 352 }
284 $result = curl_exec($ch); 353 $result = curl_exec($ch);
  354 +
  355 + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  356 + //如果分流执行失败,则走原来的逻辑
  357 + if (USE_INTER_FACE_SHUNT && $httpCode !== 200) {
  358 + curl_close($ch);
  359 + return self::_post($urlBack, $data, $returnJson, $timeout, $header, $cookie);
  360 + }
  361 +
  362 + if (!$returnJson && !empty($result)) {
  363 + $result = json_decode($result, true);
  364 + }
  365 + curl_close($ch);
  366 + $data = array();
  367 +
  368 + return $result;
  369 + }
  370 +
  371 + public static function _post($url, $data = array(), $returnJson = false, $timeout = 10, $header = array(), $cookie = array())
  372 + {
  373 + $ch = curl_init($url);
  374 + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  375 +
  376 + if (!empty($header)) {
  377 + curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  378 + } else {
  379 + curl_setopt($ch, CURLOPT_HEADER, 0);
  380 + }
  381 + if (!empty($cookie)) {
  382 + $cookie_str = array();
  383 + foreach ($cookie as $key => $val) {
  384 + $cookie_str[] = urlencode($key) . '=' . urlencode($val);
  385 + }
  386 + curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookie_str));
  387 + }
  388 + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 YOHOWEB');
  389 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  390 + curl_setopt($ch, CURLOPT_POST, true);
  391 +
  392 + // 销毁私钥参数
  393 + if (isset($data['private_key'])) {
  394 + unset($data['private_key']);
  395 + }
  396 + if (!empty($data)) {
  397 + $str = http_build_query($data, null, '&');
  398 + // 新加支持application/x-www-form-urlencoded调用方式
  399 + //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  400 + curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
  401 + }
  402 + $result = curl_exec($ch);
285 if (!$returnJson && !empty($result)) { 403 if (!$returnJson && !empty($result)) {
286 $result = json_decode($result, true); 404 $result = json_decode($result, true);
287 } 405 }
@@ -568,23 +686,25 @@ class Yohobuy @@ -568,23 +686,25 @@ class Yohobuy
568 $mod = 0; 686 $mod = 0;
569 687
570 if (isset($param['uid'])) { 688 if (isset($param['uid'])) {
571 - $uid = $param['uid'];  
572 - $mod = $param['uid'] % 1024; 689 + $uid = intval($param['uid']);
  690 + $mod = $uid % 1024;
573 } 691 }
574 692
575 if ($mod > 128 || $uid === 0) { 693 if ($mod > 128 || $uid === 0) {
576 $paseUrl = parse_url($url); 694 $paseUrl = parse_url($url);
577 if (strpos($url, 'api.')) { 695 if (strpos($url, 'api.')) {
578 - $url = 'http://' . self::$interfaceShunt['awsServers']['api'] . $paseUrl['path']. '/'; 696 + $url = 'http://' . self::$interfaceShunt['awsServers']['api'] . rtrim($paseUrl['path'], '/') . '/';
579 } else if (strpos($url, 'service.')) { 697 } else if (strpos($url, 'service.')) {
580 - $url = 'http://' . self::$interfaceShunt['awsServers']['service'] . $paseUrl['path']. '/'; 698 + $url = 'http://' . self::$interfaceShunt['awsServers']['service'] . rtrim($paseUrl['path'], '/') . '/';
581 } 699 }
582 } else { 700 } else {
583 $paseUrl = parse_url($url); 701 $paseUrl = parse_url($url);
584 if (strpos($url, 'api.')) { 702 if (strpos($url, 'api.')) {
585 - $url = 'http://' . array_rand(self::$interfaceShunt['tencentServers']['api']) . $paseUrl['path'] . '/'; 703 + $num = array_rand(self::$interfaceShunt['tencentServers']['api']);
  704 + $url = 'http://' . self::$interfaceShunt['tencentServers']['api'][$num] . rtrim($paseUrl['path'], '/') . '/';
586 } else if (strpos($url, 'service.')) { 705 } else if (strpos($url, 'service.')) {
587 - $url = 'http://' . array_rand(self::$interfaceShunt['tencentServers']['service']) . $paseUrl['path']. '/'; 706 + $num = array_rand(self::$interfaceShunt['tencentServers']['service']);
  707 + $url = 'http://' . self::$interfaceShunt['tencentServers']['service'][$num] . rtrim($paseUrl['path'], '/') . '/';
588 } 708 }
589 } 709 }
590 710