示例#1
0
 public function testGetUriWithHandlerAndParams()
 {
     $params = array('param1' => 1, 'param2' => array(2, 3));
     $this->_request->setHandler('myHandler');
     $this->_request->addParams($params);
     $this->assertEquals('myHandler?param1=1&param2=2&param2=3', $this->_request->getUri());
 }
示例#2
0
 /**
  * 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
 }
示例#3
0
 /**
  * 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
 }
示例#4
0
 /**
  * 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);
 }
示例#5
0
 /**
  * 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
 }
示例#6
0
 /**
  *
  * 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;
 }