[PHP] PHP封装CURL扩展 →→→→→进入此内容的聊天室

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

回复 "PHP封装CURL扩展"

这儿你可以回复上面这条便签

captcha