/addons/pear/Net/Curl.php

https://github.com/automatweb/automatweb_sales · PHP · 391 lines · 196 code · 26 blank · 169 comment · 2 complexity · af241a6456ebc372c58a971c2767405f MD5 · raw file

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Sterling Hughes <sterling@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Curl.php,v 1.1 2007/04/05 12:11:45 gen Exp $
  20. //
  21. // A nice friendly OO interface for CURL
  22. //
  23. require_once(PEAR_PATH.'PEAR.php');
  24. class Net_Curl extends PEAR
  25. {
  26. // {{{ Public Properties
  27. /**
  28. * The URL for cURL to work with
  29. *
  30. * @var string $url
  31. * @access public
  32. */
  33. var $url;
  34. /**
  35. * The SSL version for the transfer
  36. *
  37. * @var integer $sslVersion
  38. * @access public
  39. */
  40. var $sslVersion;
  41. /**
  42. * The filename of the SSL certificate
  43. *
  44. * @var string $sslCert
  45. * @access public
  46. */
  47. var $sslCert;
  48. /**
  49. * The password corresponding to the certificate
  50. * in the $sslCert property
  51. *
  52. * @var string $sslCertPasswd
  53. * @access public
  54. */
  55. var $sslCertPasswd;
  56. /**
  57. * User Agent string when making an HTTP request
  58. *
  59. * @var string $userAgent
  60. * @access public
  61. */
  62. var $userAgent;
  63. /**
  64. * Whether or not to include the header in the results
  65. * of the CURL transfer
  66. *
  67. * @var boolean $header
  68. */
  69. var $header = 0;
  70. /**
  71. * Whether or not to output debug information while executing a
  72. * curl transfer
  73. *
  74. * @var boolean $verbose
  75. * @access public
  76. */
  77. var $verbose = 0;
  78. /**
  79. * Whether or not to display a progress meter for the current transfer
  80. *
  81. * @var boolean $progress
  82. * @access public
  83. */
  84. var $progress = 0;
  85. /**
  86. * Whether or not to suppress error messages
  87. *
  88. * @var boolean $mute
  89. * @access public
  90. */
  91. var $mute = 1;
  92. /**
  93. * Whether or not to follow HTTP Location headers.
  94. *
  95. * @var boolean $follow_location
  96. * @access public
  97. */
  98. var $follow_location = 1;
  99. /**
  100. * Time allowed for current transfer, in seconds. 0 means no limit
  101. *
  102. * @var int $timeout
  103. * @access public
  104. */
  105. var $timeout = 0;
  106. /**
  107. * Whether or not to return the results of the
  108. * current transfer
  109. *
  110. * @var boolean $return_transfer
  111. * @access public
  112. */
  113. var $return_transfer = 1;
  114. /**
  115. * The type of transfer to perform
  116. *
  117. * @var string $type
  118. * @access public
  119. */
  120. var $type;
  121. /**
  122. * The file to upload
  123. *
  124. * @var string $file
  125. * @access public
  126. */
  127. var $file;
  128. /**
  129. * The file size of the file pointed to by the $file
  130. * property
  131. *
  132. * @var integer $file_size
  133. * @access public
  134. */
  135. var $file_size;
  136. /**
  137. * The cookies to send to the remote site
  138. *
  139. * @var array $cookies
  140. * @access public
  141. */
  142. var $cookies;
  143. /**
  144. * The fields to send in a 'POST' request
  145. *
  146. * @var array $fields
  147. * @access public
  148. */
  149. var $fields;
  150. /**
  151. * The proxy server to go through
  152. *
  153. * @var string $proxy
  154. * @access public
  155. */
  156. var $proxy;
  157. /**
  158. * The username for the Proxy server
  159. *
  160. * @var string $proxyUser
  161. * @access public
  162. */
  163. var $proxyUser;
  164. /**
  165. * The password for the Proxy server
  166. *
  167. * @var string $proxyPassword
  168. * @access public
  169. */
  170. var $proxyPassword;
  171. // }}}
  172. // {{{ Private Properties
  173. /**
  174. * The current curl handle
  175. *
  176. * @var resource $_ch
  177. * @access public
  178. */
  179. var $_ch;
  180. // }}}
  181. // {{{ Net_Curl()
  182. /**
  183. * The Net_Curl constructor, called when a new Net_Curl object
  184. * is initialized
  185. *
  186. * @param string [$url] The URL to fetch (can be set
  187. * using the $url property as well)
  188. *
  189. * @return object Net_Curl $obj A new Net_Curl object
  190. *
  191. * @access public
  192. * @author Sterling Hughes <sterling@php.net>
  193. * @since PHP 4.0.5
  194. */
  195. function Net_Curl($url = "")
  196. {
  197. if ($url) {
  198. $this->url = $url;
  199. }
  200. $ch = curl_init();
  201. if (!$ch) {
  202. $this = new PEAR_Error("Couldn't initialize a new curl handle");
  203. }
  204. $this->_ch = $ch;
  205. }
  206. // }}}
  207. // {{{ execute()
  208. /**
  209. * Executes a prepared CURL transfer
  210. *
  211. * @access public
  212. * @author Sterling Hughes <sterling@php.net>
  213. * @since PHP 4.0.5
  214. */
  215. function execute()
  216. {
  217. $ch = &$this->_ch;
  218. $ret = true;
  219. // Basic stuff
  220. $ret = curl_setopt($ch, CURLOPT_URL, $this->url);
  221. $ret = curl_setopt($ch, CURLOPT_HEADER, $this->header);
  222. // Whether or not to return the transfer contents
  223. if ($this->return_transfer && !isset($this->file)) {
  224. $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  225. }
  226. // SSL Checks
  227. if (isset($this->sslVersion)) {
  228. $ret = curl_setopt($ch, CURLOPT_SSLVERSION, $this->sslVersion);
  229. }
  230. if (isset($this->sslCert)) {
  231. $ret = curl_setopt($ch, CURLOPT_SSLCERT, $this->sslCert);
  232. }
  233. if (isset($this->sslCertPasswd)) {
  234. $ret = curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $this->sslCertPasswd);
  235. }
  236. // Proxy Related checks
  237. if (isset($this->proxy)) {
  238. $ret = curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
  239. }
  240. if (isset($this->proxyUser) || isset($this->proxyPassword)) {
  241. $proxyString = $this->proxyUser . ":" . $this->proxyPassword;
  242. $ret = curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyString);
  243. }
  244. // Transfer type
  245. if (isset($this->type)) {
  246. switch (strtolower($this->type)) {
  247. case 'post':
  248. $ret = curl_setopt($ch, CURLOPT_POST, 1);
  249. break;
  250. case 'put':
  251. $ret = curl_setopt($ch, CURLOPT_PUT, 1);
  252. break;
  253. }
  254. }
  255. // Transfer upload, etc. related
  256. if (isset($this->file)) {
  257. if (!isset($this->file_size)) {
  258. $this->file_size = filesize($this->file);
  259. }
  260. $ret = curl_setopt($ch, CURLOPT_INFILE, $this->file);
  261. $ret = curl_setopt($ch, CURLOPT_INFILESIZE, $this->file_size);
  262. }
  263. if (isset($this->fields)) {
  264. if (!isset($this->type)) {
  265. $this->type = 'post';
  266. $ret = curl_setopt($ch, CURLOPT_POST, 1);
  267. }
  268. $ret = curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields);
  269. }
  270. // Error related
  271. if ($this->progress) {
  272. $ret = curl_setopt($ch, CURLOPT_PROGRESS, 1);
  273. }
  274. if ($this->verbose) {
  275. $ret = curl_setopt($ch, CURLOPT_VERBOSE, 1);
  276. }
  277. if (!$this->mute) {
  278. $ret = curl_setopt($ch, CURLOPT_MUTE, 0);
  279. }
  280. // Other stuff
  281. $ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->follow_location);
  282. if ($this->timeout) {
  283. $ret = curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
  284. }
  285. if (isset($this->userAgent)) {
  286. $ret = curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
  287. }
  288. // Cookies and the such
  289. if (isset($this->cookies)) {
  290. foreach ($this->cookies as $name => $value) {
  291. $cookie_data .= urlencode($name) . ": " . urlencode($value) . ";";
  292. }
  293. $ret = curl_setopt($ch, CURLOPT_COOKIE, $cookie_data);
  294. }
  295. $ret = curl_exec($ch);
  296. if (!$ret) {
  297. $errObj = new PEAR_Error(curl_error($ch), curl_errno($ch));
  298. return($errObj);
  299. }
  300. return($ret);
  301. }
  302. // }}}
  303. // {{{ close()
  304. /**
  305. * Closes the curl transfer and finishes the object (kinda ;)
  306. *
  307. * @access public
  308. * @author Sterling Hughes <sterling@php.net>
  309. * @since PHP 4.0.5
  310. */
  311. function close()
  312. {
  313. if ($this->_ch) {
  314. curl_close($this->_ch);
  315. }
  316. }
  317. // }}}
  318. }
  319. // }}}
  320. ?>