Curl.class.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace KIF;
  3. /**
  4. * @name Curl.class.php
  5. * @desc curl操作类
  6. * @author 曹晓冬
  7. * @createtime 2008-12-01 14:58
  8. * @updatetime
  9. * $curl_obj = new Curl('http://www.demo.com/');
  10. * 基本使用方法
  11. * $curl_obj->init();
  12. * $curl_obj->setOptions(array("type"=>"post", "fields"=>$fields, "return"=>1, "onerror"=>1));
  13. * $result = $curl_obj->getResult();
  14. * 另外还提供了两种快速使用方法
  15. * 1.post方式
  16. * $result = $curl_obj->post(array('param' => 'value'));
  17. * 2.get方法
  18. * $result = $curl_obj->get();
  19. *
  20. */
  21. class Curl
  22. {
  23. /**
  24. * 当前curl对话
  25. * @var resource
  26. * @access private
  27. */
  28. private $ch;
  29. /**
  30. * 当前发送的地址
  31. *
  32. * @var string
  33. * @access private
  34. */
  35. private $url;
  36. /**
  37. * 调试信息
  38. *
  39. * @var string
  40. * @access private
  41. */
  42. private $debug;
  43. /**
  44. * 返回是否包括header头
  45. *
  46. * @var integer
  47. * @access private
  48. */
  49. private $header = 0;
  50. /**
  51. * 构造函数
  52. * @return void
  53. * @access public
  54. */
  55. public function __construct($url)
  56. {
  57. $this->url = $url;
  58. $this->ch = @curl_init();
  59. if (!$this->ch)
  60. {
  61. return false;
  62. }
  63. }
  64. /**
  65. * 设定返回是否包括header 头
  66. * @param integer $header 0或1,1包括,0不包括
  67. * @return void
  68. * @access public
  69. */
  70. public function setHeader($header = 0)
  71. {
  72. $this->header = $header;
  73. }
  74. /**
  75. * 初始化curl对话
  76. * @param string $url 当前发送的地址
  77. * @return boolean
  78. * @access private
  79. */
  80. public function init()
  81. {
  82. $this->basic();
  83. return true;
  84. }
  85. /**
  86. * 基本选项
  87. *
  88. * @return void
  89. * @access private
  90. */
  91. private function basic()
  92. {
  93. curl_setopt($this->ch, CURLOPT_URL, $this->url);
  94. curl_setopt($this->ch, CURLOPT_HEADER, $this->header);
  95. }
  96. /**
  97. *
  98. * 调整用curl原生的方法,直接设置option
  99. * @param unknown_type $key
  100. * @param unknown_type $value
  101. */
  102. public function setRawOption($key, $value) {
  103. curl_setopt($this->ch, $key, $value);
  104. }
  105. /**
  106. * 设置选项
  107. *
  108. * @return void
  109. * @access public
  110. */
  111. public function setOptions($options = array())
  112. {
  113. if (is_array($options))
  114. {
  115. foreach ($options as $key=>$value)
  116. {
  117. $this->$key = $value;
  118. }
  119. }
  120. //如果HTTP返回大于300, 是否显示错误
  121. if (isset($this->onerror) && $this->onerror)
  122. {
  123. curl_setopt($this->ch, CURLOPT_FAILONERROR, 1);
  124. }
  125. //是否有返回值
  126. if (isset($this->return) && $this->return == true && !isset($this->file))
  127. {
  128. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
  129. }
  130. //HTTP 认证
  131. if (isset($this->username) && $this->username != "")
  132. {
  133. curl_setopt($this->ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
  134. }
  135. //SSL 检查
  136. if (isset($this->sslVersion))
  137. {
  138. curl_setopt($this->ch, CURLOPT_SSLVERSION, $this->sslVersion);
  139. }
  140. if (isset($this->sslCert))
  141. {
  142. curl_setopt($this->ch, CURLOPT_SSLCERT, $this->sslCert);
  143. }
  144. if (isset($this->sslCertPasswd))
  145. {
  146. curl_setopt($this->ch, CURLOPT_SSLCERTPASSWD, $this->sslCertPasswd);
  147. }
  148. //代理服务器
  149. if (isset($this->proxy))
  150. {
  151. curl_setopt($this->ch, CURLOPT_PROXY, $this->proxy);
  152. }
  153. if (isset($this->proxyUser) || isset($this->proxyPassword))
  154. {
  155. curl_setopt($this->ch, CURLOPT_PROXYUSERPWD, "{$this->proxyUser}:{$this->proxyPassword}");
  156. }
  157. //传输类型
  158. if (isset($this->type))
  159. {
  160. switch (strtolower($this->type))
  161. {
  162. case "post":
  163. curl_setopt($this->ch, CURLOPT_POST, 1);
  164. break;
  165. case "put":
  166. curl_setopt($this->ch, CURLOPT_PUT, 1);
  167. break;
  168. }
  169. }
  170. //上传相关
  171. if (isset($this->file))
  172. {
  173. if (!isset($this->filesize))
  174. {
  175. $this->filesize = filesize($this->file);
  176. }
  177. curl_setopt($this->ch, CURLOPT_INFILE, $this->file);
  178. curl_setopt($this->ch, CURLOPT_INFILESIZE, $this->filesize);
  179. curl_setopt($this->ch, CURLOPT_UPLOAD, 1);
  180. }
  181. //数据发送
  182. if (isset($this->fields))
  183. {
  184. if (!is_array($this->fields))
  185. {
  186. if (!isset($this->type))
  187. {
  188. $this->type = "post";
  189. curl_setopt($this->ch, CURLOPT_POST, 1);
  190. }
  191. curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->fields);
  192. }
  193. else
  194. {
  195. if (!empty($this->fields))
  196. {
  197. $p = array();
  198. foreach ($this->fields as $key=>$value)
  199. {
  200. $p[] = $key . "=" . urlencode($value);
  201. }
  202. if (!isset($this->type))
  203. {
  204. $this->type = "post";
  205. curl_setopt($this->ch, CURLOPT_POST, 1);
  206. }
  207. curl_setopt($this->ch, CURLOPT_POSTFIELDS, implode("&", $p));
  208. }
  209. }
  210. }
  211. //错误相关
  212. if (isset($this->progress) && $this->progress == true)
  213. {
  214. curl_setopt($this->ch, CURLOPT_PROGRESS, 1);
  215. }
  216. if (isset($this->verbose) && $this->verbose == true)
  217. {
  218. curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
  219. }
  220. if (isset($this->mute) && !$this->mute)
  221. {
  222. curl_setopt($this->ch, CURLOPT_MUTE, 0);
  223. }
  224. //其它相关
  225. if (isset($this->followLocation))
  226. {
  227. curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, $this->followLocation);
  228. }
  229. if (isset($this->timeout) && $this->timeout>0)
  230. {
  231. curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->timeout);
  232. }
  233. else
  234. {
  235. curl_setopt($this->ch, CURLOPT_TIMEOUT, 20);
  236. }
  237. if (isset($this->connecttimeout) && $this->connecttimeout>0)
  238. {
  239. curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
  240. }
  241. else
  242. {
  243. curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
  244. }
  245. if (isset($this->userAgent))
  246. {
  247. curl_setopt($this->ch, CURLOPT_USERAGENT, $this->userAgent);
  248. }
  249. //cookie 相关
  250. if (isset($this->cookie))
  251. {
  252. $cookieData = "";
  253. foreach ($this->cookie as $name => $value)
  254. {
  255. $cookieData .= urlencode($name) . "=" . urlencode($value) . ";";
  256. }
  257. curl_setopt($this->ch, CURLOPT_COOKIE, $cookieData);
  258. }
  259. //http 头
  260. if (isset($this->httpHeaders))
  261. {
  262. curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->httpHeaders );
  263. }
  264. curl_setopt($this->ch, CURLOPT_HTTPAUTH, 'CURLAUTH_DIGEST');
  265. }
  266. /**
  267. * 设置返回结果选项,并返回结果
  268. * @return string 返回结果
  269. * @access public
  270. */
  271. public function getResult()
  272. {
  273. $result = curl_exec($this->ch);
  274. return $result;
  275. }
  276. /**
  277. * 关闭当前curl对话
  278. * @return void
  279. * @access public
  280. */
  281. public function close()
  282. {
  283. @curl_close($this->ch);
  284. }
  285. /**
  286. * 得到对话中产生的错误描述
  287. * @return string 错误描述
  288. * @access public
  289. */
  290. public function getError()
  291. {
  292. return curl_error($this->ch);
  293. }
  294. /**
  295. * 得到对话中产生的错误号
  296. * @return integer 错误号
  297. * @access public
  298. */
  299. public function getErrno()
  300. {
  301. return curl_errno($this->ch);
  302. }
  303. /**
  304. * 中断执行,并输出错误信息
  305. * @param string $msg 错误信息
  306. * @return void
  307. * @access private
  308. */
  309. private function halt($msg)
  310. {
  311. $message = "\n<br>信息:{$msg}";
  312. $message .= "\n<br>错误号:".$this->getErrno();
  313. $message .= "\n<br>错误:".$this->getError();
  314. echo $message;
  315. exit;
  316. }
  317. /**
  318. * 调试信息
  319. *
  320. * @return void
  321. * @access private
  322. */
  323. private function debug()
  324. {
  325. $message .= "\n<br>错误号:".$this->getErrno();
  326. $message .= "\n<br>错误:".$this->getError();
  327. $this->debug = $message;
  328. }
  329. /**
  330. * 获得以POST方式发送的结果
  331. * @param array/string $fields 发送的数据
  332. * @return string 返回的结果
  333. * @access public
  334. */
  335. public function post($fields = array())
  336. {
  337. $re = $this->init();
  338. if ($re){
  339. $this->setOptions(array("type"=>"post", "fields"=>$fields, "return"=>1, "onerror"=>1));
  340. $result = $this->getResult();
  341. //$this->close();
  342. return $result;
  343. }else{
  344. return "";
  345. }
  346. }
  347. /**
  348. * 获得以GET方式发送的结果
  349. * @return string 返回的结果
  350. * @access public
  351. */
  352. public function get()
  353. {
  354. $re = $this->init();
  355. if ($re){
  356. $this->setOptions(array("return"=>1, "onerror"=>1));
  357. $result = $this->getResult();
  358. $this->close();
  359. return $result;
  360. }else{
  361. return "";
  362. }
  363. }
  364. /**
  365. * 静态调用,获得以COOKIE方式发送的结果
  366. * @param string $url 发送的地址
  367. * @param array/string $fields 发送的数据
  368. * @return string 返回的结果
  369. * @access public
  370. */
  371. public function cookie($fields = array())
  372. {
  373. $re = $this->init();
  374. if ($re){
  375. $this->setOptions(array("cookie"=>$fields, "return"=>1, "onerror"=>1));
  376. $result = $this->getResult();
  377. $this->close();
  378. return $result;
  379. }else{
  380. return "";
  381. }
  382. }
  383. }
  384. ?>