PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Varien/Http/Adapter/Curl.php

https://bitbucket.org/acidel/buykoala
PHP | 238 lines | 125 code | 25 blank | 88 comment | 18 complexity | f0e3e5019103ac7e1fd152c5df880f1e MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Http
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * HTTP CURL Adapter
  28. *
  29. * @category Varien
  30. * @package Varien_Http
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Varien_Http_Adapter_Curl implements Zend_Http_Client_Adapter_Interface
  34. {
  35. /**
  36. * Parameters array
  37. *
  38. * @var array
  39. */
  40. protected $_config = array();
  41. protected $_resource;
  42. /**
  43. * Apply current configuration array to transport resource
  44. */
  45. protected function _applyConfig()
  46. {
  47. //curl_setopt();
  48. if (isset($this->_config['timeout'])) {
  49. curl_setopt($this->_getResource(), CURLOPT_TIMEOUT, $this->_config['timeout']);
  50. }
  51. if (isset($this->_config['maxredirects'])) {
  52. curl_setopt($this->_getResource(), CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
  53. }
  54. if (isset($this->_config['proxy'])) {
  55. curl_setopt ($this->_getResource(), CURLOPT_PROXY, $this->_config['proxy']);
  56. }
  57. if (isset($this->_config['ssl_cert'])) {
  58. curl_setopt($this->_getResource(), CURLOPT_SSLCERT, $this->_config['ssl_cert']);
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Set the configuration array for the adapter
  64. *
  65. * @param array $config
  66. */
  67. public function setConfig($config = array())
  68. {
  69. $this->_config = $config;
  70. return $this;
  71. }
  72. /**
  73. * Connect to the remote server
  74. *
  75. * @param string $host
  76. * @param int $port
  77. * @param boolean $secure
  78. * @deprecated since 1.4.0.0-rc1
  79. */
  80. public function connect($host, $port = 80, $secure = false)
  81. {
  82. //curl_setopt();
  83. if (isset($this->_config['timeout'])) {
  84. curl_setopt($this->_getResource(), CURLOPT_TIMEOUT, $this->_config['timeout']);
  85. }
  86. if (isset($this->_config['maxredirects'])) {
  87. curl_setopt($this->_getResource(), CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
  88. }
  89. if (isset($this->_config['proxy'])) {
  90. curl_setopt ($this->_getResource(), CURLOPT_PROXY, $this->_config['proxy']);
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Send request to the remote server
  96. *
  97. * @param string $method
  98. * @param Zend_Uri_Http $url
  99. * @param string $http_ver
  100. * @param array $headers
  101. * @param string $body
  102. * @return string Request as text
  103. */
  104. public function write($method, $url, $http_ver = '1.1', $headers = array(), $body = '')
  105. {
  106. if ($url instanceof Zend_Uri_Http) {
  107. $url = $url->getUri();
  108. }
  109. $this->_applyConfig();
  110. // set url to post to
  111. curl_setopt($this->_getResource(), CURLOPT_URL, $url);
  112. curl_setopt($this->_getResource(), CURLOPT_RETURNTRANSFER, true);
  113. if ($method == Zend_Http_Client::POST) {
  114. curl_setopt($this->_getResource(), CURLOPT_POST, true);
  115. curl_setopt($this->_getResource(), CURLOPT_POSTFIELDS, $body);
  116. }
  117. elseif ($method == Zend_Http_Client::GET) {
  118. curl_setopt($this->_getResource(), CURLOPT_HTTPGET, true);
  119. }
  120. if( is_array($headers) ) {
  121. curl_setopt($this->_getResource(), CURLOPT_HTTPHEADER, $headers);
  122. }
  123. curl_setopt($this->_getResource(), CURLOPT_HEADER, true);
  124. curl_setopt($this->_getResource(), CURLOPT_SSL_VERIFYPEER, 0);
  125. curl_setopt($this->_getResource(), CURLOPT_SSL_VERIFYHOST, 0);
  126. return $body;
  127. }
  128. /**
  129. * Read response from server
  130. *
  131. * @return string
  132. */
  133. public function read()
  134. {
  135. $response = curl_exec($this->_getResource());
  136. // Remove 100 and 101 responses headers
  137. if (Zend_Http_Response::extractCode($response) == 100 ||
  138. Zend_Http_Response::extractCode($response) == 101) {
  139. $response = preg_split('/^\r?$/m', $response, 2);
  140. $response = trim($response[1]);
  141. }
  142. return $response;
  143. }
  144. /**
  145. * Close the connection to the server
  146. *
  147. */
  148. public function close()
  149. {
  150. curl_close($this->_getResource());
  151. $this->_resource = null;
  152. return $this;
  153. }
  154. protected function _getResource()
  155. {
  156. if (is_null($this->_resource)) {
  157. $this->_resource = curl_init();
  158. }
  159. return $this->_resource;
  160. }
  161. public function getErrno()
  162. {
  163. return curl_errno($this->_getResource());
  164. }
  165. public function getError()
  166. {
  167. return curl_error($this->_getResource());
  168. }
  169. /**
  170. * Get information regarding a specific transfer
  171. *
  172. * @param int $opt CURLINFO option
  173. * @return mixed
  174. */
  175. public function getInfo($opt = 0)
  176. {
  177. return curl_getinfo($this->_getResource(), $opt);
  178. }
  179. /**
  180. * curl_multi_* requests support
  181. *
  182. * @param array $urls
  183. * @param array $options
  184. * @return array
  185. */
  186. public function multiRequest($urls, $options = array())
  187. {
  188. $handles = array();
  189. $result = array();
  190. $multihandle = curl_multi_init();
  191. foreach ($urls as $key => $url) {
  192. $handles[$key] = curl_init();
  193. curl_setopt($handles[$key], CURLOPT_URL, $url);
  194. curl_setopt($handles[$key], CURLOPT_HEADER, 0);
  195. curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1);
  196. if (!empty($options)) {
  197. curl_setopt_array($handles[$key], $options);
  198. }
  199. curl_multi_add_handle($multihandle, $handles[$key]);
  200. }
  201. $process = null;
  202. do {
  203. curl_multi_exec($multihandle, $process);
  204. usleep(100);
  205. } while ($process>0);
  206. foreach ($handles as $key => $handle) {
  207. $result[$key] = curl_multi_getcontent($handle);
  208. curl_multi_remove_handle($multihandle, $handle);
  209. }
  210. curl_multi_close($multihandle);
  211. return $result;
  212. }
  213. }