Source for file FileGetContents.php

Documentation is available at FileGetContents.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2007-2011, Servigistics, Inc.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *
  9.  *  - Redistributions of source code must retain the above copyright notice,
  10.  *    this list of conditions and the following disclaimer.
  11.  *  - Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  *  - Neither the name of Servigistics, Inc. nor the names of
  15.  *    its contributors may be used to endorse or promote products derived from
  16.  *    this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
  31.  * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
  32.  * @version $Id: $
  33.  *
  34.  * @package Apache
  35.  * @subpackage Solr
  36.  * @author Donovan Jimenez <djimenez@conduit-it.com>
  37.  */
  38.  
  39. // Require Apache_Solr_HttpTransport_Abstract
  40. require_once(dirname(__FILE__'/Abstract.php');
  41.  
  42. /**
  43.  * HTTP Transport implemenation that uses the builtin http URL wrappers and file_get_contents
  44.  */
  45. {
  46.     /**
  47.      * SVN Revision meta data for this class
  48.      */
  49.     const SVN_REVISION = '$Revision:  $';
  50.  
  51.     /**
  52.      * SVN ID meta data for this class
  53.      */
  54.     const SVN_ID = '$Id:  $';
  55.         
  56.     /**
  57.      * Reusable stream context resources for GET and POST operations
  58.      *
  59.      * @var resource 
  60.      */
  61.     private $_getContext$_headContext$_postContext;
  62.     
  63.     /**
  64.      * Initializes our reuseable get and post stream contexts
  65.      */
  66.     public function __construct()
  67.     {
  68.         $this->_getContext stream_context_create();
  69.         $this->_headContext stream_context_create();
  70.         $this->_postContext stream_context_create();
  71.     }
  72.  
  73.     public function performGetRequest($url$timeout false)
  74.     {
  75.         // set the timeout if specified
  76.         if ($timeout !== FALSE && $timeout 0.0)
  77.         {
  78.             // timeouts with file_get_contents seem to need
  79.             // to be halved to work as expected
  80.             $timeout = (float) $timeout 2;
  81.  
  82.             stream_context_set_option($this->_getContext'http''timeout'$timeout);
  83.         }
  84.         else
  85.         {
  86.             // use the default timeout pulled from default_socket_timeout otherwise
  87.             stream_context_set_option($this->_getContext'http''timeout'$this->getDefaultTimeout());
  88.         }
  89.  
  90.         // $http_response_headers will be updated by the call to file_get_contents later
  91.         // see http://us.php.net/manual/en/wrappers.http.php for documentation
  92.         // Unfortunately, it will still create a notice in analyzers if we don't set it here
  93.         $http_response_header null;
  94.         $responseBody @file_get_contents($urlfalse$this->_getContext);
  95.         
  96.         return $this->_getResponseFromParts($responseBody$http_response_header);
  97.     }
  98.  
  99.     public function performHeadRequest($url$timeout false)
  100.     {
  101.         stream_context_set_option($this->_headContextarray(
  102.                 'http' => array(
  103.                     // set HTTP method
  104.                     'method' => 'HEAD',
  105.  
  106.                     // default timeout
  107.                     'timeout' => $this->getDefaultTimeout()
  108.                 )
  109.             )
  110.         );
  111.  
  112.         // set the timeout if specified
  113.         if ($timeout !== FALSE && $timeout 0.0)
  114.         {
  115.             // timeouts with file_get_contents seem to need
  116.             // to be halved to work as expected
  117.             $timeout = (float) $timeout 2;
  118.  
  119.             stream_context_set_option($this->_headContext'http''timeout'$timeout);
  120.         }
  121.         
  122.         // $http_response_headers will be updated by the call to file_get_contents later
  123.         // see http://us.php.net/manual/en/wrappers.http.php for documentation
  124.         // Unfortunately, it will still create a notice in analyzers if we don't set it here
  125.         $http_response_header null;
  126.         $responseBody @file_get_contents($urlfalse$this->_headContext);
  127.  
  128.         return $this->_getResponseFromParts($responseBody$http_response_header);
  129.     }
  130.     
  131.     public function performPostRequest($url$rawPost$contentType$timeout false)
  132.     {
  133.         stream_context_set_option($this->_postContextarray(
  134.                 'http' => array(
  135.                     // set HTTP method
  136.                     'method' => 'POST',
  137.  
  138.                     // Add our posted content type
  139.                     'header' => "Content-Type: $contentType",
  140.  
  141.                     // the posted content
  142.                     'content' => $rawPost,
  143.  
  144.                     // default timeout
  145.                     'timeout' => $this->getDefaultTimeout()
  146.                 )
  147.             )
  148.         );
  149.  
  150.         // set the timeout if specified
  151.         if ($timeout !== FALSE && $timeout 0.0)
  152.         {
  153.             // timeouts with file_get_contents seem to need
  154.             // to be halved to work as expected
  155.             $timeout = (float) $timeout 2;
  156.  
  157.             stream_context_set_option($this->_postContext'http''timeout'$timeout);
  158.         }
  159.  
  160.         // $http_response_header will be updated by the call to file_get_contents later
  161.         // see http://us.php.net/manual/en/wrappers.http.php for documentation
  162.         // Unfortunately, it will still create a notice in analyzers if we don't set it here
  163.         $http_response_header null;
  164.         $responseBody @file_get_contents($urlfalse$this->_postContext);
  165.         
  166.         // reset content of post context to reclaim memory
  167.         stream_context_set_option($this->_postContext'http''content''');
  168.         
  169.         return $this->_getResponseFromParts($responseBody$http_response_header);
  170.     }
  171.     
  172.     private function _getResponseFromParts($rawResponse$httpHeaders)
  173.     {
  174.         //Assume 0, false as defaults
  175.         $status 0;
  176.         $contentType false;
  177.  
  178.         //iterate through headers for real status, type, and encoding
  179.         if (is_array($httpHeaders&& count($httpHeaders0)
  180.         {
  181.             //look at the first headers for the HTTP status code
  182.             //and message (errors are usually returned this way)
  183.             //
  184.             //HTTP 100 Continue response can also be returned before
  185.             //the REAL status header, so we need look until we find
  186.             //the last header starting with HTTP
  187.             //
  188.             //the spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1
  189.             //
  190.             //Thanks to Daniel Andersson for pointing out this oversight
  191.             while (isset($httpHeaders[0]&& substr($httpHeaders[0]04== 'HTTP')
  192.             {
  193.                 // we can do a intval on status line without the "HTTP/1.X " to get the code
  194.                 $status intval(substr($httpHeaders[0]9));
  195.  
  196.                 // remove this from the headers so we can check for more
  197.                 array_shift($httpHeaders);
  198.             }
  199.  
  200.             //Look for the Content-Type response header and determine type
  201.             //and encoding from it (if possible - such as 'Content-Type: text/plain; charset=UTF-8')
  202.             foreach ($httpHeaders as $header)
  203.             {
  204.                 // look for the header that starts appropriately
  205.                 if (strncasecmp($header'Content-Type:'13== 0)
  206.                 {
  207.                     $contentType substr($header13);
  208.                     break;
  209.                 }
  210.             }
  211.         }
  212.         
  213.         return new Apache_Solr_HttpTransport_Response($status$contentType$rawResponse);
  214.     }
  215. }

Documentation generated on Wed, 04 May 2011 11:01:14 -0400 by phpDocumentor 1.4.3