File Source for Curl.php

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

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