PHP封装CURL扩展类实例

这篇文章主要介绍了PHP封装CURL扩展类,实例分析了基于curl发送post、get请求及操作cookie等相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了PHP封装CURL扩展类,分享给大家供大家参考,具体如下:

  1. <?php
  2. /**
  3. * @description: 封装CURL扩展
  4. * @date: 2014-07-28 16:04
  5. */
  6. /**
  7. * @编码规范
  8. * @class 类名首字母大写,类名为多个单词, 每个大字首字母大写 eg: class Curl , class CurlPage
  9. * @variable 变量名小写, 变量名为多个单词, 每个单词小写,使用下划线_分割 eg: $curl_result
  10. * @function 函数名与类名规则相同 eg: function SendRequest
  11. * @params 函数形参规则与变量名相同
  12. * @class-variable 成员变量,以下划线结尾,多个单词使用下划线分隔. eg: private $host_name_
  13. */
  14. /**
  15. * @要求
  16. *
  17. */
  18. class Curl{
  19. /**
  20. * @请求的host
  21. */
  22. private $host_;
  23. /**
  24. * @curl 句柄
  25. */
  26. private $ch_;
  27. /**
  28. * @超时限制时间
  29. */
  30. const time_=5;
  31. /**
  32. * @请求的设置
  33. */
  34. private $options_;
  35. /**
  36. * @保存请求头信息
  37. */
  38. private $request_header_;
  39. /**
  40. * @保存响应头信息
  41. */
  42. private $response_header_;
  43. /**
  44. * @body_ 用于保存curl请求返回的结果
  45. */
  46. private $body_;
  47. /**
  48. * @读取cookie
  49. */
  50. private $cookie_file_;
  51. /**
  52. * @写入cookie
  53. */
  54. private $cookie_jar_;
  55. /**
  56. * @todo proxy
  57. * @构造函数,初始化CURL回话
  58. */
  59. public function Start($url){
  60. $this->ch_ = curl_init($url);
  61. curl_setopt($this->ch_, CURLOPT_HEADER, 1);
  62. curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 1 );
  63. }
  64. /**
  65. * @返回响应头
  66. */
  67. public function ResponseHeader($url){
  68. if (!function_exists('http_parse_headers')) {
  69. function http_parse_headers ($raw_headers){
  70. $headers = array();
  71. foreach (explode("\n", $raw_headers) as $i => $h) {
  72. $h = explode(':', $h, 2);
  73. if (isset($h[1])) {
  74. if(!isset($headers[$h[0]])) {
  75. $headers[$h[0]] = trim($h[1]);
  76. } else if(is_array($headers[$h[0]])) {
  77. $tmp = array_merge($headers[$h[0]],array(trim($h[1])));
  78. $headers[$h[0]] = $tmp;
  79. } else {
  80. $tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));
  81. $headers[$h[0]] = $tmp;
  82. }
  83. }
  84. }
  85. return $headers;
  86. }
  87. }
  88. $this->Start($url);
  89. curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
  90. $this->body_=$this->Execx();
  91. $header_size = curl_getinfo($this->ch_, CURLINFO_HEADER_SIZE);
  92. $this->response_header_ = substr($this->body_, $start = 0, $offset = $header_size);
  93. $this->response_header_ = http_parse_headers($this->response_header_);
  94. print_r($this->response_header_);
  95. return $this->Close($this->body_);
  96. }
  97. /**
  98. * @读取cookie
  99. */
  100. public function LoadCookie($url,$cookie_file){
  101. $this->Start($url);
  102. curl_setopt($this->ch_, CURLOPT_COOKIE, 1);
  103. curl_setopt($this->ch_, CURLOPT_COOKIEFILE , $cookie_file);
  104. $this->body_=$this->Execx();
  105. return $this->Close($this->body_);
  106. }
  107. /**
  108. * @写入cookie
  109. */
  110. public function SaveCookie($url){
  111. $this->Start($url);
  112. curl_setopt($this->ch_, CURLOPT_COOKIE, 1);
  113. curl_setopt($this->ch_, CURLOPT_COOKIEFILE ,'cookie.txt');
  114. curl_setopt($this->ch_, CURLOPT_COOKIEJAR , 'cookie.txt');
  115. $this->body_=$this->Execx();
  116. return $this->Close($this->body_);
  117. }
  118. /**
  119. * @设置HEADER
  120. */
  121. public function SetHeader($headers = null){
  122. if (is_array($headers) && count($headers) > 0) {
  123. curl_setopt($this->ch_, CURLOPT_HTTPHEADER, $headers);
  124. }
  125. }
  126. /**
  127. * @GET请求
  128. */
  129. public function Get($url, array $params = array()) {
  130. if ($params) {
  131. if (strpos($url, '?')) {
  132. $url .= "&".http_build_query($params);
  133. }
  134. else {
  135. $url .= "?".http_build_query($params);
  136. }
  137. }
  138. $this->Start($url);
  139. curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
  140. if (strpos($url, 'https') === 0) {
  141. curl_setopt($this->ch_, CURLOPT_SSL_VERIFYHOST, 0);
  142. curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);
  143. }
  144. $this->body_=$this->Execx();
  145. return $this->Close($this->body_);
  146. }
  147. /**
  148. * @POST请求
  149. */
  150. public function Post($url, array $params = array()) {
  151. $this->Start($url);
  152. curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);
  153. curl_setopt($this->ch_, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
  154. curl_setopt($this->ch_, CURLOPT_POST, true);
  155. curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
  156. if ($params) {
  157. curl_setopt($this->ch_, CURLOPT_POSTFIELDS, http_build_query($params));
  158. }
  159. $this->body_=$this->Execx();
  160. return $this->Close($this->body_);
  161. }
  162. /**
  163. * @tips: google http head 方法
  164. */
  165. public function Head($url, array $params = array()) {
  166. $this->Start($url);
  167. curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
  168. curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 0);
  169. curl_setOpt($this->ch_,CURLOPT_NOBODY, true);
  170. $this->body_=$this->Execx();
  171. return $this->Close($this->body_);
  172. }
  173. /**
  174. * @执行CURL会话
  175. */
  176. public function Execx(){
  177. return curl_exec($this->ch_);
  178. }
  179. /**
  180. * @关闭CURL句柄
  181. */
  182. public function Close($body_){
  183. if ($body_ === false) {
  184. echo "CURL Error: " . curl_error($body_);
  185. return false;
  186. }
  187. curl_close($this->ch_);
  188. return $body_;
  189. }
  190. }

希望本文所述对大家的php程序设计有所帮助。