/** * @param string $url * @param AuthProvider $auth * @return object representing the requested resource * @throws InvalidAPIResponseException * @throws NoResponseException */ public static function request($url, $auth) { // init curl request $req = curl_init(); curl_setopt($req, CURLOPT_RETURNTRANSFER, true); $attempts = 0; $res = false; while ($res === false && $attempts < 3) { curl_setopt($req, CURLOPT_URL, $url . 'access_token=' . $auth->getToken()); $res = curl_exec($req); if (curl_getinfo($req, CURLINFO_HTTP_CODE) == 401 && $attempts < 2) { $auth->getNewToken(); $res = false; } elseif ($res) { $res = json_decode($res); if ($res === null) { $res = false; } } $attempts++; } // validate response if ($res === false) { throw new NoResponseException("Could not load endpoint " . $url); } elseif ($res == null) { echo curl_getinfo($req, CURLOPT_URL); throw new InvalidAPIResponseException("Invalid Response on " . $url); } else { return $res; } }
/** * getNewToken() * * function that generates a new access token and returns it * * @return String new access token * @throws InvalidCredentialsException */ public function getNewToken() { if ($this->_authProvider instanceof AuthProvider) { $this->_token = $this->_authProvider->getNewToken(); return $this->_token; } else { throw new InvalidCredentialsException("Could not get token from sub auth provider"); } }
/** * Return auth provider code by id * * @param type $code * @return type * @throws \CException */ public static function getIdByCode($code) { $row = AuthProvider::model()->find('code = :code', array(':code' => $code)); if (!$row) { throw new \CException("Undefined auth provider code {$code}"); } return $row->id; }