/** * Call a remote REST web service URI and return the Zend_Http_Response object * * @param string $path The path to append to the URI * @throws Zend_Service_Exception * @return void */ private final function _prepareRest($path, $query) { // Get the URI object and configure it if (!$this->_uri instanceof Zend_Uri) { throw new Zend_Service_Exception('URI object must be set before performing call'); } // @todo this needs to be revisited if ($path[0] != '/' && $this->_uri[strlen($this->_uri) - 1] != '/') { $path = '/' . $path; } $this->_uri->setPath($path); if (!is_null($query) && is_string($query)) { $this->_uri->setQueryString($query); } elseif (is_array($query)) { $this->_uri->setQueryArray($query); } /** * Get the HTTP client and configure it for the endpoint URI. Do this each time * because the Zend_Http_Client instance is shared among all Zend_Service_Abstract subclasses. */ self::getHttpClient()->setUri($this->_uri); }
/** * Send the HTTP request and return an HTTP response object * * @param string $method * @return Zend_Http_Response */ public function request($method = null) { if (!$this->uri instanceof Zend_Uri_Http) { throw new Zend_Http_Client_Exception('No valid URI has been passed to the client'); } if ($method) { $this->setMethod($method); } $this->redirectCounter = 0; $response = null; // Make sure the adapter is loaded if ($this->adapter == null) { $this->loadAdapter($this->config['adapter']); } // Send the first request. If redirected, continue. do { // Clone the URI and add the additional GET parameters to it $uri = clone $this->uri; if (!empty($this->paramsGet)) { $query = $uri->getQuery(); if (!empty($query)) { $query .= '&'; } $query .= http_build_query($this->paramsGet, null, '&'); $uri->setQuery($query); } $body = $this->prepare_body(); $headers = $this->prepare_headers(); // Open the connection, send the request and read the response $this->adapter->connect($uri->getHost(), $uri->getPort(), $uri->getScheme() == 'https' ? true : false); $this->last_request = $this->adapter->write($this->method, $uri, $this->config['httpversion'], $headers, $body); $response = $this->adapter->read(); if (!$response) { throw new Zend_Http_Client_Exception('Unable to read response, or response is empty'); } $response = Zend_Http_Response::fromString($response); if ($this->config['storeresponse']) { $this->last_response = $response; } // Load cookies into cookie jar if (isset($this->cookiejar)) { $this->cookiejar->addCookiesFromResponse($response, $uri); } // If we got redirected, look for the Location header if ($response->isRedirect() && ($location = $response->getHeader('location'))) { // Check whether we send the exact same request again, or drop the parameters // and send a GET request if ($response->getStatus() == 303 || !$this->config['strictredirects'] && ($response->getStatus() == 302 || $response->getStatus() == 301)) { $this->resetParameters(); $this->setMethod(self::GET); } // If we got a well formed absolute URI if (Zend_Uri_Http::check($location)) { $this->setHeaders('host', null); $this->setUri($location); } else { // Split into path and query and set the query if (strpos($location, '?') !== false) { list($location, $query) = explode('?', $location, 2); } else { $query = ''; } $this->uri->setQueryString($query); // Else, if we got just an absolute path, set it if (strpos($location, '/') === 0) { $this->uri->setPath($location); // Else, assume we have a relative path } else { // Get the current path directory, removing any trailing slashes $path = rtrim(dirname($this->uri->getPath()), "/"); $this->uri->setPath($path . '/' . $location); } } ++$this->redirectCounter; } else { // If we didn't get any location, stop redirecting break; } } while ($this->redirectCounter < $this->config['maxredirects']); return $response; }