Curl.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // Require Apache_Solr_HttpTransport_Abstract
  39. require_once(dirname(__FILE__) . '/Abstract.php');
  40. /**
  41. * A Curl based HTTP transport. Uses a single curl session for all requests.
  42. */
  43. class Apache_Solr_HttpTransport_Curl extends Apache_Solr_HttpTransport_Abstract
  44. {
  45. /**
  46. * SVN Revision meta data for this class
  47. */
  48. const SVN_REVISION = '$Revision:$';
  49. /**
  50. * SVN ID meta data for this class
  51. */
  52. const SVN_ID = '$Id:$';
  53. /**
  54. * Curl Session Handle
  55. *
  56. * @var resource
  57. */
  58. private $_curl;
  59. /**
  60. * Initializes a curl session
  61. */
  62. public function __construct()
  63. {
  64. // initialize a CURL session
  65. $this->_curl = curl_init();
  66. // set common options that will not be changed during the session
  67. curl_setopt_array($this->_curl, array(
  68. // return the response body from curl_exec
  69. CURLOPT_RETURNTRANSFER => true,
  70. // get the output as binary data
  71. CURLOPT_BINARYTRANSFER => true,
  72. // we do not need the headers in the output, we get everything we need from curl_getinfo
  73. CURLOPT_HEADER => false
  74. ));
  75. }
  76. /**
  77. * Closes a curl session
  78. */
  79. function __destruct()
  80. {
  81. // close our curl session
  82. curl_close($this->_curl);
  83. }
  84. public function performGetRequest($url, $timeout = false)
  85. {
  86. // check the timeout value
  87. if ($timeout === false || $timeout <= 0.0)
  88. {
  89. // use the default timeout
  90. $timeout = $this->getDefaultTimeout();
  91. }
  92. // set curl GET options
  93. curl_setopt_array($this->_curl, array(
  94. // make sure we're returning the body
  95. CURLOPT_NOBODY => false,
  96. // make sure we're GET
  97. CURLOPT_HTTPGET => true,
  98. // set the URL
  99. CURLOPT_URL => $url,
  100. // set the timeout
  101. CURLOPT_TIMEOUT => $timeout
  102. ));
  103. // make the request
  104. $responseBody = curl_exec($this->_curl);
  105. // get info from the transfer
  106. $statusCode = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);
  107. $contentType = curl_getinfo($this->_curl, CURLINFO_CONTENT_TYPE);
  108. return new Apache_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
  109. }
  110. public function performHeadRequest($url, $timeout = false)
  111. {
  112. // check the timeout value
  113. if ($timeout === false || $timeout <= 0.0)
  114. {
  115. // use the default timeout
  116. $timeout = $this->getDefaultTimeout();
  117. }
  118. // set curl HEAD options
  119. curl_setopt_array($this->_curl, array(
  120. // this both sets the method to HEAD and says not to return a body
  121. CURLOPT_NOBODY => true,
  122. // set the URL
  123. CURLOPT_URL => $url,
  124. // set the timeout
  125. CURLOPT_TIMEOUT => $timeout
  126. ));
  127. // make the request
  128. $responseBody = curl_exec($this->_curl);
  129. // get info from the transfer
  130. $statusCode = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);
  131. $contentType = curl_getinfo($this->_curl, CURLINFO_CONTENT_TYPE);
  132. return new Apache_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
  133. }
  134. public function performPostRequest($url, $postData, $contentType, $timeout = false)
  135. {
  136. // check the timeout value
  137. if ($timeout === false || $timeout <= 0.0)
  138. {
  139. // use the default timeout
  140. $timeout = $this->getDefaultTimeout();
  141. }
  142. // set curl POST options
  143. curl_setopt_array($this->_curl, array(
  144. // make sure we're returning the body
  145. CURLOPT_NOBODY => false,
  146. // make sure we're POST
  147. CURLOPT_POST => true,
  148. // set the URL
  149. CURLOPT_URL => $url,
  150. // set the post data
  151. CURLOPT_POSTFIELDS => $postData,
  152. // set the content type
  153. CURLOPT_HTTPHEADER => array("Content-Type: {$contentType}"),
  154. // set the timeout
  155. CURLOPT_TIMEOUT => $timeout
  156. ));
  157. // make the request
  158. $responseBody = curl_exec($this->_curl);
  159. // get info from the transfer
  160. $statusCode = curl_getinfo($this->_curl, CURLINFO_HTTP_CODE);
  161. $contentType = curl_getinfo($this->_curl, CURLINFO_CONTENT_TYPE);
  162. return new Apache_Solr_HttpTransport_Response($statusCode, $contentType, $responseBody);
  163. }
  164. }