Source for file CurlNoReuse.php

Documentation is available at CurlNoReuse.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.  * An alternative Curl HTTP transport that opens and closes a curl session for
  44.  * every request. This isn't the recommended way to use curl, but some version of
  45.  * PHP have memory issues.
  46.  */
  47. {
  48.     /**
  49.      * SVN Revision meta data for this class
  50.      */
  51.     const SVN_REVISION = '$Revision:$';
  52.  
  53.     /**
  54.      * SVN ID meta data for this class
  55.      */
  56.     const SVN_ID = '$Id:$';
  57.  
  58.     public function performGetRequest($url$timeout false)
  59.     {
  60.         // check the timeout value
  61.         if ($timeout === false || $timeout <= 0.0)
  62.         {
  63.             // use the default timeout
  64.             $timeout $this->getDefaultTimeout();
  65.         }
  66.         
  67.         $curl curl_init();
  68.  
  69.         // set curl GET options
  70.         curl_setopt_array($curlarray(
  71.             // return the response body from curl_exec
  72.             CURLOPT_RETURNTRANSFER => true,
  73.  
  74.             // get the output as binary data
  75.             CURLOPT_BINARYTRANSFER => true,
  76.  
  77.             // we do not need the headers in the output, we get everything we need from curl_getinfo
  78.             CURLOPT_HEADER => false,
  79.             
  80.             // set the URL
  81.             CURLOPT_URL => $url,
  82.  
  83.             // set the timeout
  84.             CURLOPT_TIMEOUT => $timeout
  85.         ));
  86.  
  87.         // make the request
  88.         $responseBody curl_exec($curl);
  89.  
  90.         // get info from the transfer
  91.         $statusCode curl_getinfo($curlCURLINFO_HTTP_CODE);
  92.         $contentType curl_getinfo($curlCURLINFO_CONTENT_TYPE);
  93.         
  94.         // close our curl session - we're done with it
  95.         curl_close($curl);
  96.  
  97.         return new Apache_Solr_HttpTransport_Response($statusCode$contentType$responseBody);
  98.     }
  99.  
  100.     public function performHeadRequest($url$timeout false)
  101.     {
  102.         // check the timeout value
  103.         if ($timeout === false || $timeout <= 0.0)
  104.         {
  105.             // use the default timeout
  106.             $timeout $this->getDefaultTimeout();
  107.         }
  108.         
  109.         $curl curl_init();
  110.  
  111.         // set curl HEAD options
  112.         curl_setopt_array($curlarray(
  113.             // return the response body from curl_exec
  114.             CURLOPT_RETURNTRANSFER => true,
  115.  
  116.             // get the output as binary data
  117.             CURLOPT_BINARYTRANSFER => true,
  118.  
  119.             // we do not need the headers in the output, we get everything we need from curl_getinfo
  120.             CURLOPT_HEADER => false,
  121.             
  122.             // this both sets the method to HEAD and says not to return a body
  123.             CURLOPT_NOBODY => true,
  124.  
  125.             // set the URL
  126.             CURLOPT_URL => $url,
  127.  
  128.             // set the timeout
  129.             CURLOPT_TIMEOUT => $timeout
  130.         ));
  131.  
  132.         // make the request
  133.         $responseBody curl_exec($curl);
  134.  
  135.         // get info from the transfer
  136.         $statusCode curl_getinfo($curlCURLINFO_HTTP_CODE);
  137.         $contentType curl_getinfo($curlCURLINFO_CONTENT_TYPE);
  138.         
  139.         // close our curl session - we're done with it
  140.         curl_close($curl);
  141.  
  142.         return new Apache_Solr_HttpTransport_Response($statusCode$contentType$responseBody);
  143.     }
  144.  
  145.     public function performPostRequest($url$postData$contentType$timeout false)
  146.     {
  147.         // check the timeout value
  148.         if ($timeout === false || $timeout <= 0.0)
  149.         {
  150.             // use the default timeout
  151.             $timeout $this->getDefaultTimeout();
  152.         }
  153.  
  154.         $curl curl_init();
  155.         
  156.         // set curl POST options
  157.         curl_setopt_array($curlarray(
  158.             // return the response body from curl_exec
  159.             CURLOPT_RETURNTRANSFER => true,
  160.  
  161.             // get the output as binary data
  162.             CURLOPT_BINARYTRANSFER => true,
  163.  
  164.             // we do not need the headers in the output, we get everything we need from curl_getinfo
  165.             CURLOPT_HEADER => false,
  166.             
  167.             // make sure we're POST
  168.             CURLOPT_POST => true,
  169.  
  170.             // set the URL
  171.             CURLOPT_URL => $url,
  172.  
  173.             // set the post data
  174.             CURLOPT_POSTFIELDS => $postData,
  175.  
  176.             // set the content type
  177.             CURLOPT_HTTPHEADER => array("Content-Type: {$contentType}"),
  178.  
  179.             // set the timeout
  180.             CURLOPT_TIMEOUT => $timeout
  181.         ));
  182.  
  183.         // make the request
  184.         $responseBody curl_exec($curl);
  185.  
  186.         // get info from the transfer
  187.         $statusCode curl_getinfo($curlCURLINFO_HTTP_CODE);
  188.         $contentType curl_getinfo($curlCURLINFO_CONTENT_TYPE);
  189.  
  190.         // close our curl session - we're done with it
  191.         curl_close($curl);
  192.  
  193.         return new Apache_Solr_HttpTransport_Response($statusCode$contentType$responseBody);
  194.     }
  195. }

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