CurlNoReuse.php 5.8 KB

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