File indexing completed on 2024-06-23 05:55:43

0001 <?php
0002 
0003 /**
0004  * Copyright (c) 2007-2011, Servigistics, Inc.
0005  * All rights reserved.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions are met:
0009  *
0010  *  - Redistributions of source code must retain the above copyright notice,
0011  *    this list of conditions and the following disclaimer.
0012  *  - Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *  - Neither the name of Servigistics, Inc. nor the names of
0016  *    its contributors may be used to endorse or promote products derived from
0017  *    this software without specific prior written permission.
0018  *
0019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0029  * POSSIBILITY OF SUCH DAMAGE.
0030  *
0031  * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
0032  * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
0033  * @version $Id: $
0034  *
0035  * @package Apache
0036  * @subpackage Solr
0037  * @author Timo Schmidt <timo.schmidt@aoemedia.de>, Donovan Jimenez <djimenez@conduit-it.com>
0038  */
0039 require_once 'Zend/Service/Solr/HttpTransport/Abstract.php';
0040 
0041 /**
0042  * A Curl based HTTP transport. Uses a single curl session for all requests.
0043  */
0044 class Zend_Service_Solr_HttpTransport_Curl extends Zend_Service_Solr_HttpTransport_Abstract {
0045     /**
0046      * SVN Revision meta data for this class
0047      */
0048     const SVN_REVISION = '$Revision:$';
0049 
0050     /**
0051      * SVN ID meta data for this class
0052      */
0053     const SVN_ID = '$Id:$';
0054 
0055     /**
0056      * Curl Session Handle
0057      *
0058      * @var resource
0059      */
0060     private $_curl;
0061 
0062     /**
0063      * Initializes a curl session
0064      */
0065     public function __construct() {
0066         // initialize a CURL session
0067         $this->_curl = curl_init();
0068 
0069         // set common options that will not be changed during the session
0070         curl_setopt_array($this->_curl, array(
0071             // return the response body from curl_exec
0072             CURLOPT_RETURNTRANSFER => true,
0073             // get the output as binary data
0074             CURLOPT_BINARYTRANSFER => true,
0075             // we do not need the headers in the output, we get everything we need from curl_getinfo
0076             CURLOPT_HEADER => false
0077         ));
0078     }
0079 
0080     /**
0081      * Closes a curl session
0082      */
0083     function __destruct() {
0084         // close our curl session
0085         curl_close($this->_curl);
0086     }
0087 
0088     public function performGetRequest($url, $timeout = false) {
0089         // check the timeout value
0090         if ($timeout === false || $timeout <= 0.0) {
0091             // use the default timeout
0092             $timeout = $this->getDefaultTimeout();
0093         }
0094 
0095         // set curl GET options
0096         curl_setopt_array($this->_curl, array(
0097             // make sure we're returning the body
0098             CURLOPT_NOBODY => false,
0099             // make sure we're GET
0100             CURLOPT_HTTPGET => true,
0101             // set the URL
0102             CURLOPT_URL => $url,
0103             // set the timeout
0104             CURLOPT_TIMEOUT => $timeout
0105         ));
0106 
0107         // make the request
0108         $responseBody = curl_exec($this->_curl);
0109 
0110         // get info from the transfer
0111         $statusCode = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);
0112         $contentType = curl_getinfo($this->_curl, CURLINFO_CONTENT_TYPE);
0113 
0114         return new Zend_Service_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
0115     }
0116 
0117     public function performHeadRequest($url, $timeout = false) {
0118         // check the timeout value
0119         if ($timeout === false || $timeout <= 0.0) {
0120             // use the default timeout
0121             $timeout = $this->getDefaultTimeout();
0122         }
0123 
0124         // set curl HEAD options
0125         curl_setopt_array($this->_curl, array(
0126             // this both sets the method to HEAD and says not to return a body
0127             CURLOPT_NOBODY => true,
0128             // set the URL
0129             CURLOPT_URL => $url,
0130             // set the timeout
0131             CURLOPT_TIMEOUT => $timeout
0132         ));
0133 
0134         // make the request
0135         $responseBody = curl_exec($this->_curl);
0136 
0137         // get info from the transfer
0138         $statusCode = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);
0139         $contentType = curl_getinfo($this->_curl, CURLINFO_CONTENT_TYPE);
0140 
0141         return new Zend_Service_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
0142     }
0143 
0144     public function performPostRequest($url, $postData, $contentType, $timeout = false) {
0145         // check the timeout value
0146         if ($timeout === false || $timeout <= 0.0) {
0147             // use the default timeout
0148             $timeout = $this->getDefaultTimeout();
0149         }
0150 
0151         // set curl POST options
0152         curl_setopt_array($this->_curl, array(
0153             // make sure we're returning the body
0154             CURLOPT_NOBODY => false,
0155             // make sure we're POST
0156             CURLOPT_POST => true,
0157             // set the URL
0158             CURLOPT_URL => $url,
0159             // set the post data
0160             CURLOPT_POSTFIELDS => $postData,
0161             // set the content type
0162             CURLOPT_HTTPHEADER => array("Content-Type: {$contentType}"),
0163             // set the timeout
0164             CURLOPT_TIMEOUT => $timeout
0165         ));
0166 
0167         // make the request
0168         $responseBody = curl_exec($this->_curl);
0169 
0170         // get info from the transfer
0171         $statusCode = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);
0172         $contentType = curl_getinfo($this->_curl, CURLINFO_CONTENT_TYPE);
0173 
0174         return new Zend_Service_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
0175     }
0176 
0177 }