Authored by 梁志锋

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

... ... @@ -150,14 +150,16 @@ class Yohobuy
{
$isApi = false;
$isService = false;
$urlBack = $url;
if (USE_INTER_FACE_SHUNT) {
if (strpos($url, 'api.')) {
$isApi = true;
$url = self::_getUrl($url, $data);
} else if (strpos($url, 'service.')) {
$isService = true;
$url = self::_getUrl($url, $data);
}
$url = self::_getUrl($url, $data);
}
// 销毁私钥参数
... ... @@ -199,6 +201,14 @@ class Yohobuy
}
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
//如果分流执行失败,则走原来的逻辑
if (USE_INTER_FACE_SHUNT && $httpCode !== 200) {
curl_close($ch);
return self::_get($urlBack, $data, $cache, $returnJson, $timeout, $userAgent, $useGzip);
}
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
... ... @@ -221,6 +231,63 @@ class Yohobuy
return $result;
}
private static function _get($url, $data = array(), $cache = false, $returnJson = false, $timeout = 5, $userAgent = null, $useGzip = true)
{
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$url = self::httpBuildQuery($url, $data);
} //echo $url;
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($url, 'master');
if (!empty($result)) {
return $result;
}
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($useGzip) {
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
}
if (!empty($userAgent)) {
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
/* 开启缓存的情况 */
if ($cache && USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($url, 'slave');
}
// 接口调用成功时,这里会设置一级(master)和二级(slave)的缓存数据.
else {
Cache::set($url, $result, $cache);
}
}
return $result;
}
/**
* post提交数据
*
... ... @@ -236,14 +303,16 @@ class Yohobuy
{
$isApi = false;
$isService = false;
$urlBack = $url;
if (USE_INTER_FACE_SHUNT) {
if (strpos($url, 'api.')) {
$isApi = true;
$url = self::_getUrl($url, $data);
} else if (strpos($url, 'service.')) {
$isService = true;
$url = self::_getUrl($url, $data);
}
$url = self::_getUrl($url, $data);
}
$ch = curl_init($url);
... ... @@ -282,6 +351,55 @@ class Yohobuy
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
}
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//如果分流执行失败,则走原来的逻辑
if (USE_INTER_FACE_SHUNT && $httpCode !== 200) {
curl_close($ch);
return self::_post($urlBack, $data, $returnJson, $timeout, $header, $cookie);
}
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
curl_close($ch);
$data = array();
return $result;
}
public static function _post($url, $data = array(), $returnJson = false, $timeout = 10, $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_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 YOHOWEB');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
// 销毁私钥参数
if (isset($data['private_key'])) {
unset($data['private_key']);
}
if (!empty($data)) {
$str = http_build_query($data, null, '&');
// 新加支持application/x-www-form-urlencoded调用方式
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
}
$result = curl_exec($ch);
if (!$returnJson && !empty($result)) {
$result = json_decode($result, true);
}
... ... @@ -568,23 +686,25 @@ class Yohobuy
$mod = 0;
if (isset($param['uid'])) {
$uid = $param['uid'];
$mod = $param['uid'] % 1024;
$uid = intval($param['uid']);
$mod = $uid % 1024;
}
if ($mod > 128 || $uid === 0) {
$paseUrl = parse_url($url);
if (strpos($url, 'api.')) {
$url = 'http://' . self::$interfaceShunt['awsServers']['api'] . $paseUrl['path']. '/';
$url = 'http://' . self::$interfaceShunt['awsServers']['api'] . rtrim($paseUrl['path'], '/') . '/';
} else if (strpos($url, 'service.')) {
$url = 'http://' . self::$interfaceShunt['awsServers']['service'] . $paseUrl['path']. '/';
$url = 'http://' . self::$interfaceShunt['awsServers']['service'] . rtrim($paseUrl['path'], '/') . '/';
}
} else {
$paseUrl = parse_url($url);
if (strpos($url, 'api.')) {
$url = 'http://' . array_rand(self::$interfaceShunt['tencentServers']['api']) . $paseUrl['path'] . '/';
$num = array_rand(self::$interfaceShunt['tencentServers']['api']);
$url = 'http://' . self::$interfaceShunt['tencentServers']['api'][$num] . rtrim($paseUrl['path'], '/') . '/';
} else if (strpos($url, 'service.')) {
$url = 'http://' . array_rand(self::$interfaceShunt['tencentServers']['service']) . $paseUrl['path']. '/';
$num = array_rand(self::$interfaceShunt['tencentServers']['service']);
$url = 'http://' . self::$interfaceShunt['tencentServers']['service'][$num] . rtrim($paseUrl['path'], '/') . '/';
}
}
... ...