You are here

public function Apache_Solr_Response::__construct in Apache Solr Search 5

Constructor. Takes the raw HTTP response body and the exploded HTTP headers

Parameters

string $rawResponse:

array $httpHeaders:

boolean $createDocuments Whether to convert the documents json_decoded as stdClass instances to Apache_Solr_Document instances:

boolean $collapseSingleValueArrays Whether to make multivalued fields appear as single values:

File

SolrPhpClient/Apache/Solr/Response.php, line 79

Class

Apache_Solr_Response
Represents a Solr response. Parses the raw response into a set of stdClass objects and associative arrays for easy access.

Code

public function __construct($rawResponse, $httpHeaders = array(), $createDocuments = true, $collapseSingleValueArrays = true) {

  //Assume 0, 'Communication Error', utf-8, and  text/plain
  $status = 0;
  $statusMessage = 'Communication Error';
  $type = 'text/plain';
  $encoding = 'UTF-8';

  //iterate through headers for real status, type, and encoding
  if (is_array($httpHeaders) && count($httpHeaders) > 0) {

    //look at the first headers for the HTTP status code

    //and message (errors are usually returned this way)

    //

    //HTTP 100 Continue response can also be returned before

    //the REAL status header, so we need look until we find

    //the last header starting with HTTP

    //

    //the spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1

    //

    //Thanks to Daniel Andersson for pointing out this oversight
    while (isset($httpHeaders[0]) && substr($httpHeaders[0], 0, 4) == 'HTTP') {
      $parts = split(' ', substr($httpHeaders[0], 9), 2);
      $status = $parts[0];
      $statusMessage = trim($parts[1]);
      array_shift($httpHeaders);
    }

    //Look for the Content-Type response header and determine type

    //and encoding from it (if possible - such as 'Content-Type: text/plain; charset=UTF-8')
    foreach ($httpHeaders as $header) {
      if (strncasecmp($header, 'Content-Type:', 13) == 0) {

        //split content type value into two parts if possible
        $parts = split(';', substr($header, 13), 2);
        $type = trim($parts[0]);
        if ($parts[1]) {

          //split the encoding section again to get the value
          $parts = split('=', $parts[1], 2);
          if ($parts[1]) {
            $encoding = trim($parts[1]);
          }
        }
        break;
      }
    }
  }
  $this->_rawResponse = $rawResponse;
  $this->_type = $type;
  $this->_encoding = $encoding;
  $this->_httpStatus = $status;
  $this->_httpStatusMessage = $statusMessage;
  $this->_createDocuments = (bool) $createDocuments;
  $this->_collapseSingleValueArrays = (bool) $collapseSingleValueArrays;
}