Source for file Curl.php

Documentation is available at Curl.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 Timo Schmidt <timo.schmidt@aoemedia.de>, Donovan Jimenez <djimenez@conduit-it.com>
  37.  */
  38.  
  39. // Require Apache_Solr_HttpTransport_Abstract
  40. require_once(dirname(__FILE__'/Abstract.php');
  41.  
  42. /**
  43.  * A Curl based HTTP transport. Uses a single curl session for all requests.
  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.      * Curl Session Handle
  58.      *
  59.      * @var resource 
  60.      */
  61.     private $_curl;
  62.  
  63.     /**
  64.      * Initializes a curl session
  65.      */
  66.     public function __construct()
  67.     {
  68.         // initialize a CURL session
  69.         $this->_curl curl_init();
  70.  
  71.         // set common options that will not be changed during the session
  72.         curl_setopt_array($this->_curlarray(
  73.             // return the response body from curl_exec
  74.             CURLOPT_RETURNTRANSFER => true,
  75.  
  76.             // get the output as binary data
  77.             CURLOPT_BINARYTRANSFER => true,
  78.  
  79.             // we do not need the headers in the output, we get everything we need from curl_getinfo
  80.             CURLOPT_HEADER => false
  81.         ));
  82.     }
  83.  
  84.     /**
  85.      * Closes a curl session
  86.      */
  87.     function __destruct()
  88.     {
  89.         // close our curl session
  90.         curl_close($this->_curl);
  91.     }
  92.  
  93.     public function performGetRequest($url$timeout false)
  94.     {
  95.         // check the timeout value
  96.         if ($timeout === false || $timeout <= 0.0)
  97.         {
  98.             // use the default timeout
  99.             $timeout $this->getDefaultTimeout();
  100.         }
  101.  
  102.         // set curl GET options
  103.         curl_setopt_array($this->_curlarray(
  104.             // make sure we're returning the body
  105.             CURLOPT_NOBODY => false,
  106.  
  107.             // make sure we're GET
  108.             CURLOPT_HTTPGET => true,
  109.  
  110.             // set the URL
  111.             CURLOPT_URL => $url,
  112.  
  113.             // set the timeout
  114.             CURLOPT_TIMEOUT => $timeout
  115.         ));
  116.  
  117.         // make the request
  118.         $responseBody curl_exec($this->_curl);
  119.  
  120.         // get info from the transfer
  121.         $statusCode curl_getinfo($this->_curlCURLINFO_HTTP_CODE);
  122.         $contentType curl_getinfo($this->_curlCURLINFO_CONTENT_TYPE);
  123.  
  124.         return new Apache_Solr_HttpTransport_Response($statusCode$contentType$responseBody);
  125.     }
  126.  
  127.     public function performHeadRequest($url$timeout false)
  128.     {
  129.         // check the timeout value
  130.         if ($timeout === false || $timeout <= 0.0)
  131.         {
  132.             // use the default timeout
  133.             $timeout $this->getDefaultTimeout();
  134.         }
  135.  
  136.         // set curl HEAD options
  137.         curl_setopt_array($this->_curlarray(
  138.             // this both sets the method to HEAD and says not to return a body
  139.             CURLOPT_NOBODY => true,
  140.  
  141.             // set the URL
  142.             CURLOPT_URL => $url,
  143.  
  144.             // set the timeout
  145.             CURLOPT_TIMEOUT => $timeout
  146.         ));
  147.  
  148.         // make the request
  149.         $responseBody curl_exec($this->_curl);
  150.  
  151.         // get info from the transfer
  152.         $statusCode curl_getinfo($this->_curlCURLINFO_HTTP_CODE);
  153.         $contentType curl_getinfo($this->_curlCURLINFO_CONTENT_TYPE);
  154.  
  155.         return new Apache_Solr_HttpTransport_Response($statusCode$contentType$responseBody);
  156.     }
  157.  
  158.     public function performPostRequest($url$postData$contentType$timeout false)
  159.     {
  160.         // check the timeout value
  161.         if ($timeout === false || $timeout <= 0.0)
  162.         {
  163.             // use the default timeout
  164.             $timeout $this->getDefaultTimeout();
  165.         }
  166.  
  167.         // set curl POST options
  168.         curl_setopt_array($this->_curlarray(
  169.             // make sure we're returning the body
  170.             CURLOPT_NOBODY => false,
  171.  
  172.             // make sure we're POST
  173.             CURLOPT_POST => true,
  174.  
  175.             // set the URL
  176.             CURLOPT_URL => $url,
  177.  
  178.             // set the post data
  179.             CURLOPT_POSTFIELDS => $postData,
  180.  
  181.             // set the content type
  182.             CURLOPT_HTTPHEADER => array("Content-Type: {$contentType}"),
  183.  
  184.             // set the timeout
  185.             CURLOPT_TIMEOUT => $timeout
  186.         ));
  187.  
  188.         // make the request
  189.         $responseBody curl_exec($this->_curl);
  190.  
  191.         // get info from the transfer
  192.         $statusCode curl_getinfo($this->_curlCURLINFO_HTTP_CODE);
  193.         $contentType curl_getinfo($this->_curlCURLINFO_CONTENT_TYPE);
  194.  
  195.         return new Apache_Solr_HttpTransport_Response($statusCode$contentType$responseBody);
  196.     }
  197. }

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