Request for a http/https resource
public static request ( string $url, array $params = [], string $httpMethod = 'GET', boolean $multi = false ) : string | false | ||
$url | string | Url to request |
$params | array | Parameters for the request |
$httpMethod | string | Http method, 'GET' or 'POST' |
$multi | boolean | Whether it's a multipart POST request |
return | string | false | Returns string if success, or false if failed |
public function getLoggedInUser($access_token) { $result = BaiduUtils::request(self::$BD_OAUTH2_ENDPOINTS['getLoginUser'], array('access_token' => $access_token, 'redirect_uri' => $this->redirectUri), 'GET'); if ($result) { $result = json_decode($result, true); if (isset($result['error_msg'])) { BaiduUtils::setError($result['error_code'], $result['error_msg']); return false; } return $result; } return false; }
private function doBatchRun($useHttps = true) { $batchQueue = $this->batchQueue[$useHttps ? 0 : 1]; if (empty($batchQueue)) { return; } $num = count($batchQueue); $params = array(); foreach ($batchQueue as $item) { $params[] = $item['i']; } $json = json_encode($params); $serialOnly = $this->batchMode === self::BATCH_MODE_SERIAL_ONLY; $params = array('method' => $json, 'serial_only' => $serialOnly); if ($useHttps) { $params['access_token'] = $this->getAccessToken(); $domain = self::$BD_OPENAPI_DEFAULT_DOMAINS['rest']; } else { $params['client_id'] = $this->getClientId(); $domain = self::$BD_OPENAPI_DEFAULT_DOMAINS['public']; } $result = BaiduUtils::request($domain . '/batch/run', $params, 'POST'); if ($result === false) { throw new BaiduException('failed to call batch/run api: ' . BaiduUtils::errmsg(), BaiduUtils::errno()); } $result = $this->converJson2Array($result); if (is_array($result) && isset($result['error_code'])) { throw new BaiduException('failed to call batch/run api: ' . $result['error_msg'], $result['error_code']); } for ($i = 0; $i < $num; $i++) { $item = $batchQueue[$i]; $itemResult = $result[$i]; if (is_array($itemResult) && isset($itemResult['error_code'])) { throw new BaiduException('failed to call ' . $item['i']['path'] . ' api: ' . $itemResult['error_msg'], $itemResult['error_code']); } $item['r'] = $itemResult; } }
/** * Make an oauth access token request * * The parameters: * - client_id: The client identifier, just use api key * - response_type: 'token' or 'code' * - redirect_uri: the url to go to after a successful login * - scope: The scope of the access request expressed as a list of space-delimited, case sensitive strings. * - state: An opaque value used by the client to maintain state between the request and callback. * - display: login page style, 'page', 'popup', 'touch' or 'mobile' * * @param array $params oauth request parameters * @return mixed returns access token info if success, or false if failed */ public function makeAccessTokenRequest($params) { $result = BaiduUtils::request(self::$BD_OAUTH2_ENDPOINTS['token'], $params, 'POST'); if ($result) { $result = json_decode($result, true); if (isset($result['error_description'])) { BaiduUtils::setError($result['error'], $result['error_description']); return false; } return $result; } return false; }