/** * Execute request * * @param Solarium_Client_Request $request * @return array */ protected function _getData($request) { // @codeCoverageIgnoreStart $uri = $this->getBaseUri() . $request->getUri(); $method = $request->getMethod(); $options = $this->_createOptions($request); if ($method == Solarium_Client_Request::METHOD_POST) { if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; } $httpResponse = http_post_data($uri, $request->getRawData(), $options); } else { if ($method == Solarium_Client_Request::METHOD_GET) { $httpResponse = http_get($uri, $options); } else { if ($method == Solarium_Client_Request::METHOD_HEAD) { $httpResponse = http_head($uri, $options); } else { throw new Solarium_Exception("unsupported method: {$method}"); } } } $headers = array(); $data = ''; if ($message = http_parse_message($httpResponse)) { $data = $message->body; if ($firstPositionOfCRLF = strpos($httpResponse, "\r\n\r\n")) { $headersAsString = substr($httpResponse, 0, $firstPositionOfCRLF); $headers = explode("\n", $headersAsString); } } return array($data, $headers); // @codeCoverageIgnoreEnd }
/** * Execute request * * @param Solarium_Client_Request $request * @return array */ protected function _getData($request) { // @codeCoverageIgnoreStart $uri = $this->getBaseUri() . $request->getUri(); $method = $request->getMethod(); $options = $this->_createOptions($request); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $uri); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeout']); if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; } if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; } if (count($options['headers'])) { $arr = array(); foreach ($options['headers'] as $k => $v) { $arr[] = $k . ": " . $v; } curl_setopt($ch, CURLOPT_HTTPHEADER, $arr); } if ($method == Solarium_Client_Request::METHOD_POST) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getRawData()); $httpResponse = curl_exec($ch); } else { if ($method == Solarium_Client_Request::METHOD_GET) { curl_setopt($ch, CURLOPT_HTTPGET, true); $httpResponse = curl_exec($ch); } else { if ($method == Solarium_Client_Request::METHOD_HEAD) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); $httpResponse = curl_exec($ch); } else { throw new Solarium_Exception("unsupported method: {$method}"); } } } $headers = array(); $data = ''; if ($httpResponse !== false) { $data = $httpResponse; $info = curl_getinfo($ch); $headers = array(); $headers[] = 'HTTP/1.1 ' . $info['http_code'] . ' OK'; } return array($data, $headers); // @codeCoverageIgnoreEnd }
/** * Create a stream context for a request * * @param Solarium_Client_Request $request * @return resource */ public function createContext($request) { $method = $request->getMethod(); $context = stream_context_create(array('http' => array('method' => $method, 'timeout' => $this->getTimeout()))); if ($method == Solarium_Client_Request::METHOD_POST) { $data = $request->getRawData(); if (null !== $data) { stream_context_set_option($context, 'http', 'content', $data); $request->addHeader('Content-Type: text/xml; charset=UTF-8'); } } $headers = $request->getHeaders(); if (count($headers) > 0) { stream_context_set_option($context, 'http', 'header', implode("\r\n", $headers)); } return $context; }
/** * Execute a Solr request using the Zend_Http_Client instance * * @param Solarium_Client_Request $request * @return Solarium_Client_Response */ public function execute($request) { $client = $this->getZendHttp(); $client->setMethod($request->getMethod()); $client->setUri($this->getBaseUri() . $request->getUri()); $client->setHeaders($request->getHeaders()); $client->setRawData($request->getRawData()); $response = $client->request(); // throw an exception in case of a HTTP error if ($response->isError()) { throw new Solarium_Client_HttpException($response->getMessage(), $response->getStatus()); } if ($request->getMethod() == Solarium_Client_Request::METHOD_HEAD) { $data = ''; } else { $data = $response->getBody(); } // this is used because getHeaders doesn't return the HTTP header... $headers = explode("\n", $response->getHeadersAsString()); return new Solarium_Client_Response($data, $headers); }
public function testSetAndGetMethod() { $this->_request->setMethod(Solarium_Client_Request::METHOD_POST); $this->assertEquals(Solarium_Client_Request::METHOD_POST, $this->_request->getMethod()); }
/** * Create curl handle for a request * * @param Solarium_Client_Request $request * @return resource */ public function createHandle($request) { // @codeCoverageIgnoreStart $uri = $this->getBaseUri() . $request->getUri(); $method = $request->getMethod(); $options = $this->_createOptions($request); $handler = curl_init(); curl_setopt($handler, CURLOPT_URL, $uri); curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1); curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true); curl_setopt($handler, CURLOPT_CONNECTTIMEOUT, $options['timeout']); if ($proxy = $this->getOption('proxy')) { curl_setopt($handler, CURLOPT_PROXY, $proxy); } if (!isset($options['headers']['Content-Type'])) { $options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; } if (count($options['headers'])) { $headers = array(); foreach ($options['headers'] as $key => $value) { $headers[] = $key . ": " . $value; } curl_setopt($handler, CURLOPT_HTTPHEADER, $headers); } if ($method == Solarium_Client_Request::METHOD_POST) { curl_setopt($handler, CURLOPT_POST, true); curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData()); } else { if ($method == Solarium_Client_Request::METHOD_GET) { curl_setopt($handler, CURLOPT_HTTPGET, true); } else { if ($method == Solarium_Client_Request::METHOD_HEAD) { curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'HEAD'); } else { throw new Solarium_Exception("unsupported method: {$method}"); } } } return $handler; // @codeCoverageIgnoreEnd }
/** * * adapt Solarium_Client_Request to HttpRequest * * {@link http://us.php.net/manual/en/http.constants.php * HTTP Predefined Constant} * * @param Solarium_Client_Request $request * @param HttpRequest */ public function toHttpRequest($request) { $url = $this->getBaseUri() . $request->getUri(); $httpRequest = new HttpRequest($url); $headers = array(); foreach ($request->getHeaders() as $headerLine) { list($header, $value) = explode(':', $headerLine); if ($header = trim($header)) { $headers[$header] = trim($value); } } switch ($request->getMethod()) { case Solarium_Client_Request::METHOD_GET: $method = HTTP_METH_GET; break; case Solarium_Client_Request::METHOD_POST: $method = HTTP_METH_POST; $httpRequest->setBody($request->getRawData()); if (!isset($headers['Content-Type'])) { $headers['Content-Type'] = 'text/xml; charset=utf-8'; } break; case Solarium_Client_Request::METHOD_HEAD: $method = HTTP_METH_HEAD; break; default: throw new Solarium_Exception('Unsupported method: ' . $request->getMethod()); } $httpRequest->setMethod($method); $httpRequest->setOptions(array('timeout' => $this->getTimeout())); $httpRequest->setHeaders($headers); return $httpRequest; }
/** * Event hook to adjust client settings just before query execution * * @param Solarium_Query $query * @param Solarium_Client_Request $request * @return void */ public function postCreateRequest($query, $request) { $queryString = $request->getQueryString(); if ($request->getMethod() == Solarium_Client_Request::METHOD_GET && strlen($queryString) > $this->getMaxQueryStringLength()) { $request->setMethod(Solarium_Client_Request::METHOD_POST); $request->setRawData($queryString); $request->clearParams(); $request->addHeader('Content-Type: application/x-www-form-urlencoded'); } }