public function apiCall($path, $method = 'GET', $params = null, $json = false) { //Prepare request URL $url = $this->oauth_endpoint . $path; //Obtain access token for authentication $token = $this->oauth2->getAccessToken(); //Prepare cURL options $options[CURLOPT_RETURNTRANSFER] = true; $options[CURLOPT_CONNECTTIMEOUT] = 10; $options[CURLOPT_TIMEOUT] = 30; $options[CURLOPT_USERAGENT] = $this->user_agent; $options[CURLOPT_CUSTOMREQUEST] = $method; $options[CURLOPT_HTTPHEADER][] = "Authorization: " . $token['token_type'] . " " . $token['access_token']; if ($json) { $options[CURLOPT_HTTPHEADER][] = "Content-Type: application/json"; } //Execution is placed in a loop in case CAPTCHA is required. do { //Prepare URL or POST parameters if (isset($params)) { if ($method == 'GET') { $url .= '?' . http_build_query($params); } else { $options[CURLOPT_POSTFIELDS] = $params; } } //Build cURL object $ch = curl_init($url); curl_setopt_array($ch, $options); //Wait on rate limiter if necessary $this->ratelimiter->wait(); //Print request URL for debug if ($this->debug) { echo $url . "\n"; } //Send request and close connection $response_raw = curl_exec($ch); curl_close($ch); //Parse response $response = json_decode($response_raw); if ($json_error = json_last_error()) { $response = $response_raw; } if (isset($response->json->captcha)) { $params['iden'] = $response->json->captcha; $params['captcha'] = $this->getCaptchaResponse($response->json->captcha); $needs_captcha = $params['captcha'] === 'skip' ? false : true; } else { $needs_captcha = false; } } while ($needs_captcha); return $response; }
public function getAccessToken($client_id = '', $secret = '', $redirect_url = '', $code = '') { $result = parent::getAccessToken($client_id, $secret, $redirect_url, $code); $result = json_decode($result, true); if (isset($result['error'])) { $this->error = $result['error'] . ' ' . $result['error_description']; return false; } else { $this->access_token = $result['access_token']; return $result; } }
/** * 获取 AccessToken * @param string $code */ public function getAccessToken($code) { $token = parent::getAccessToken($code); $this->uid = $token['uid']; return $token; }