/** * Makes an HTTP request using PHP's CURL * * @param RESTian_Request $request * @param RESTian_Response $response * @return RESTian_Response */ function make_request($request, $response) { $ch = curl_init(); curl_setopt_array($ch, array(CURLOPT_URL => $request->get_url(), CURLOPT_USERAGENT => $request->client->get_user_agent(), CURLOPT_HTTPHEADER => $request->get_curl_headers(), CURLOPT_POST => false, CURLOPT_HEADER => false, CURLOPT_TIMEOUT => '30', CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => $request->sslverify, CURLOPT_SSL_VERIFYHOST => true === $request->sslverify ? 2 : false)); if (!$request->omit_body) { $response->body = trim(curl_exec($ch)); } $info = curl_getinfo($ch); $response->status_code = $info['http_code']; if (0 != curl_errno($ch)) { $response->set_http_error(curl_errno($ch), curl_error($ch)); } if (!$request->omit_result) { $response->result = (object) array('info' => $info, 'version' => curl_version(), 'error' => curl_error($ch), 'errno' => curl_errno($ch)); } curl_close($ch); return $response; }
/** * Call the API. * * On success (HTTP status == 200) $this->error_code and $this->error_msg will be false. * On failure (HTTP status != 200) $this->error_code and $this->error_msg will contain error information. * * On success or failure, $this->response will contain the response captured by HTTP agent * except when username and password are not passed as part of auth. * * @return object|RESTian_Response */ function make_request() { $response = new RESTian_Response(array('request' => $this)); $api = $this->client; /** * Assign request & response to API so they are accessible inside the auth_provider */ $api->request = $this; $api->response = $response; $auth_provider = $api->get_auth_provider(); if ($this->needs_authentication() && !$this->has_authentication()) { $response->set_error('NO_AUTH', $this->service); } else { $http_agent = RESTian::get_new_http_agent($this->client->http_agent); $this->assign_settings(); $response = $http_agent->make_request($this, $response); if ($response->is_http_error()) { /** * See if we can provide more than one error type here. */ $msg = 'There was a problem reaching %s when calling the %s. Please try again later or contact the site\'s administrator.'; $response->set_error('API_FAIL', sprintf($msg, $this->client->api_name, $this->service->service_name)); } else { if ('authenticate' == $response->request->service->service_name) { $handled = $auth_provider->authenticated($response); } else { $handled = $auth_provider->handle_response($response); } if (!$handled) { // @todo Add more HTTP status code responses as we better understand the use-cases. switch ($response->status_code) { case '200': /** * @var RESTian_Parser_Base $parser */ $parser = RESTian::get_new_parser($this->service->content_type, $this, $response); if ($parser instanceof RESTian_Parser_Base) { $response->data = $parser->parse($response->body); } break; case '401': $response->set_error('BAD_AUTH', $this->service); break; default: /** * See if we can provide more than one error type here. */ $response->set_error('UNKNOWN', 'Unexpected API response code: ' . $response->status_code); break; } } if ($this->omit_body) { $response->body = null; } if ($this->omit_result) { $response->result = null; } } } return $response; }
/** * Authenticate against the API. * * @param bool|array $credentials * @return RESTian_Response */ function authenticate($credentials = false) { if (!$credentials) { $credentials = $this->_credentials; } if (!$this->_credentials) { $this->_credentials = $credentials; } $this->_auth_service = $this->get_auth_service(); /** * @var RESTian_Auth_Provider_Base */ $auth_provider = $this->get_auth_provider(); $this->request = new RESTian_Request(null, array('credentials' => $credentials, 'service' => $this->_auth_service)); if (!$this->is_credentials($credentials)) { $response = new RESTian_Response(array('request' => $this->request)); $response->set_error('NO_AUTH', 'Credentials not provided. Please enter your credentials.'); } else { /** * @var RESTian_Response $response */ $response = $this->make_request($this->request); $response->authenticated = $auth_provider->authenticated($response); if (!$response->authenticated) { $response->set_error('BAD_AUTH', $auth_provider->message); } else { $auth_provider->capture_grant($response); } } return $response; }