public function &api($uri, $params = array(), $httpMethod = 'GET', $type = 'rest')
{
if (substr($uri, 0, 8) === 'https://') {
//apis using https + access_token
$params = array_merge(array('access_token' => $this->getAccessToken()), $params);
} elseif (substr($uri, 0, 7) === 'http://') {
//apis using http + client_id
$params = array_merge(array('client_id' => $this->getClientId()), $params);
} else {
if (substr($uri, 0, 6) === '/rest/') {
//apis using https + access_token and default domain
$uri = self::$BD_OPENAPI_DEFAULT_DOMAINS['rest'] . $uri;
$params = array_merge(array('access_token' => $this->getAccessToken()), $params);
} elseif (substr($uri, 0, 8) === '/public/') {
//apis using http + client and default domain
$uri = self::$BD_OPENAPI_DEFAULT_DOMAINS['public'] . $uri;
$params = array_merge(array('client_id' => $this->getClientId()), $params);
} elseif ($type === 'rest') {
$uri = self::$BD_OPENAPI_DEFAULT_PREFIXS['rest'] . $uri;
$params = array_merge(array('access_token' => $this->getAccessToken()), $params);
} elseif ($type === 'public') {
$uri = self::$BD_OPENAPI_DEFAULT_PREFIXS['public'] . $uri;
$params = array_merge(array('client_id' => $this->getClientId()), $params);
} else {
BaiduUtils::setError(-1, 'Invalid params for ' . __METHOD__ . ": uri[{$uri}] type[{$type}]");
return false;
}
}
if ($this->batchQueue === null) {
$result = BaiduUtils::request($uri, $params, $httpMethod);
if ($result !== false) {
$result = $this->converJson2Array($result);
if (is_array($result) && isset($result['error_code'])) {
BaiduUtils::setError(-1, 'failed to call baidu openapi: error_code[' . $result['error_code'] . '] error_msg[' . $result['error_msg'] . ']');
return false;
}
}
} else {
// batch run
$result = null;
unset($params['access_token']);
unset($params['client_id']);
$query = http_build_query($params, '', '&');
$parts = parse_url($uri);
$item = array('domain' => $parts['host'], 'path' => $parts['path'], 'params' => $parts['query'] ? $parts['query'] . '&' . $query : $query, 'http_method' => $httpMethod);
if ($parts['scheme'] === 'https') {
$this->batchQueue[0][] = array('i' => $item, 'r' => &$result);
} else {
$this->batchQueue[1][] = array('i' => $item, 'r' => &$result);
}
}
return $result;
}