Sends an HTTP request.
public request ( string $method, string | Horde_Url $uri = null, string | array $data = null, array $headers = [] ) : Horde_Http_Response_Base | ||
$method | string | HTTP request method (GET, PUT, etc.) |
$uri | string | Horde_Url | URI to request, if different from $this->uri |
$data | string | array | Request data. Array of form data that will be encoded automatically, or a raw string. |
$headers | array | Any headers specific to this request. They will be combined with $this->_headers, and override headers of the same name for this request only. |
return | Horde_Http_Response_Base |
/** * Sends a request and parses the response. * * @param string $method A HTTP request method (uppercase). * @param string $namespace An API namespace. * @param array $params URL parameters. * @param array|string $data Request data. * * @return array The decoded result data or null if no data has been * returned but the request was still successful. * @throws Horde_OpenXchange_Exception. */ protected function _request($method, $namespace, $params, $data = array()) { $uri = new Horde_Url($this->_uri . '/' . $namespace, true); try { $headers = array(); if (isset($this->_cookies)) { $headers['Cookie'] = implode('; ', $this->_cookies); } if ($method == 'GET') { $params = array_merge($params, $data); $data = null; } $response = $this->_client->request($method, (string) $uri->add($params), $data, $headers); if ($cookies = $response->getHeader('set-cookie')) { if (!is_array($cookies)) { $cookies = array($cookies); } foreach ($cookies as $cookie) { $cookie = preg_split('/;\\s*/', $cookie); for ($i = 1, $c = count($cookie); $i < $c; $i++) { list($key, $value) = explode('=', $cookie[$i]); if ($key == 'Expires') { $expire = new Horde_Date($value); if ($expire->before(time())) { continue 2; } break; } } $this->_cookies[] = $cookie[0]; } } $body = $response->getBody(); $data = json_decode($body, true); if (!$data) { if ($response->code == 200) { return; } throw new Horde_OpenXchange_Exception($body); } if (isset($data['error'])) { $e = new Horde_OpenXchange_Exception($data['error']); $e->details = $data; throw $e; } return $data; } catch (Horde_Http_Exception $e) { throw new Horde_OpenXchange_Exception($e); } }
/** * Performs an actual HTTP request, and returns the result. * * If the specified url is relative, it will be expanded based on the base * url. * * The returned array contains 3 keys: * * body - the response body * * httpCode - a HTTP code (200, 404, etc) * * headers - a list of response http headers. The header names have * been lowercased. * * @param string $method * @param string $url * @param string $body * @param array $headers * * @return array * @throws Horde_Dav_Exception */ public function request($method, $url = '', $body = null, $headers = array()) { $url = $this->getAbsoluteUrl($url); $this->_http->{'request.redirects'} = 5; $this->_http->{'request.verifyPeer'} = $this->verifyPeer; if ($this->proxy) { $this->_http->{'request.proxyServer'} = $this->proxy; } if ($this->userName && $this->authType) { if ($this->authType & self::AUTH_BASIC) { $this->_http->{'request.authenticationScheme'} = Horde_Http::AUTH_BASIC; } if ($this->authType & self::AUTH_DIGEST) { $this->_http->{'request.authenticationScheme'} = Horde_Http::AUTH_DIGEST; } $this->_http->{'request.username'} = $this->userName; $this->_http->{'request.password'} = $this->password; } // Not supported by Horde_Http_Client yet: // $this->trustedCertificates; if ($method == 'HEAD') { $body = null; } try { $result = $this->_http->request($method, $url, $body, $headers); } catch (Horde_Http_Exception $e) { throw new Horde_Dav_Exception($e); } if (isset($result->headers['dav']) && is_array($result->headers['dav'])) { $result->headers['dav'] = implode(', ', $result->headers['dav']); } $response = array('body' => $result->getBody(), 'statusCode' => $result->code, 'headers' => $result->headers, 'url' => $result->uri); if ($response['statusCode'] >= 400) { switch ($response['statusCode']) { case 400: throw new Horde_Dav_Exception('Bad request', $response['statusCode']); case 401: throw new Horde_Dav_Exception('Not authenticated', $response['statusCode']); case 402: throw new Horde_Dav_Exception('Payment required', $response['statusCode']); case 403: throw new Horde_Dav_Exception('Forbidden', $response['statusCode']); case 404: throw new Horde_Dav_Exception('Resource not found.', $response['statusCode']); case 405: throw new Horde_Dav_Exception('Method not allowed', $response['statusCode']); case 409: throw new Horde_Dav_Exception('Conflict', $response['statusCode']); case 412: throw new Horde_Dav_Exception('Precondition failed', $response['statusCode']); case 416: throw new Horde_Dav_Exception('Requested Range Not Satisfiable', $response['statusCode']); case 500: throw new Horde_Dav_Exception('Internal server error', $response['statusCode']); case 501: throw new Horde_Dav_Exception('Not Implemented', $response['statusCode']); case 507: throw new Horde_Dav_Exception('Insufficient storage', $response['statusCode']); default: throw new Horde_Dav_Exception('HTTP error response. (errorcode ' . $response['statusCode'] . ')', $response['statusCode']); } } return $response; }