/** * 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; }
/** * @param RESTian_Request $request * * @return array */ function get_args($request) { $args = array('method' => $request->http_method, 'headers' => $request->get_headers(), 'body' => $request->get_body(), 'sslverify' => $request->sslverify, 'user-agent' => $request->client->get_user_agent()); return $args; }
/** * @param RESTian_Request $request */ function prepare_request($request) { $credentials = $request->get_credentials(); $auth = base64_encode("{$credentials['username']}:{$credentials['password']}"); $request->add_header('Authorization', "Basic {$auth}"); }
/** * @param string|RESTian_Service $service * @param string $method HTTP methods 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD' and RESTian method 'DO' * @param null|array $vars * @param null|array|object $args * @return object|RESTian_Response * @throws Exception */ function call_service($service, $method = 'GET', $vars = null, $args = null) { $this->initialize_client(); $args['service'] = is_object($service) ? $service : $this->get_service($service); $request = new RESTian_Request($vars, $args); /** * @todo This will need to be updated when we have a use-case where actions require 'POST' * @todo ...or maybe we'll evolve RESTian to deprecate actions? */ $request->http_method = 'DO' == $method ? 'GET' : $method; if (isset($args['credentials'])) { $this->_credentials = $args['credentials']; } $request->set_credentials($this->_credentials); if (isset($args['grant'])) { $this->_grant = $args['grant']; } $request->set_grant($this->_grant); return $this->process_response($this->make_request($request), $vars, $args); }