CurlHelper.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace network;
  3. /**
  4. * Created by PhpStorm.
  5. * User: Administrator
  6. * Date: 2022/8/18
  7. * Time: 13:00
  8. */
  9. class CurlHelper
  10. {
  11. private $curl;
  12. private $showResponseHeader = false;
  13. private $responseHeader;
  14. private $showRequestHeader = false;
  15. private $requestHeader;
  16. private $response;
  17. private $responseBody;
  18. private $httpCode;
  19. private $error;
  20. public function __construct($url, $params = [])
  21. {
  22. $this->curl = curl_init();
  23. curl_setopt($this->curl, CURLOPT_URL, $url);
  24. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);//要求结果为字符串且输出到屏幕上
  25. curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true);//跟踪爬取重定向页面
  26. curl_setopt($this->curl, CURLOPT_MAXREDIRS, 10);//表示最多允许重定向多少次
  27. curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  28. $this->setUserAgent();
  29. $this->setTimeout();
  30. $this->setEncoding();
  31. //跳过https证书验证
  32. if (substr($url, 0, 5) == 'https') {
  33. $this->sslVerify();
  34. }
  35. $this->setParams($params);
  36. }
  37. /**
  38. * @param string $userAgent
  39. */
  40. public function setUserAgent($userAgent = '')
  41. {
  42. if (!$userAgent) {
  43. $userAgent = empty($_SERVER['HTTP_USER_AGENT']) ? 'Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)' : $_SERVER['HTTP_USER_AGENT'];//模拟用户使用的浏览器
  44. }
  45. curl_setopt($this->curl, CURLOPT_USERAGENT, $userAgent);
  46. }
  47. /**
  48. * 设置超时
  49. * @param int $connect_timeout
  50. * @param int $timeout
  51. */
  52. public function setTimeout($connect_timeout = 10, $timeout = 300)
  53. {
  54. curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $connect_timeout);//连接超时时间
  55. curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout);//数据传输的最大允许时间, 默认为0,意思是永远不会断开链接
  56. }
  57. /**
  58. * 设置请求参数
  59. * @param $params array
  60. */
  61. public function setParams($params)
  62. {
  63. if ($params) {
  64. // post请求
  65. curl_setopt($this->curl, CURLOPT_POST, true);
  66. // $post_data为array类型时multipart/form­data请求,string类型时application/x-www-form-urlencoded
  67. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
  68. } else {
  69. // get请求
  70. curl_setopt($this->curl, CURLOPT_HTTPGET, true);
  71. }
  72. }
  73. /**
  74. * 设置代理
  75. * @param $proxy_host string
  76. * @param $proxy_port int
  77. */
  78. public function setProxy($proxy_host, $proxy_port)
  79. {
  80. curl_setopt($this->curl, CURLOPT_PROXY, $proxy_host);
  81. curl_setopt($this->curl, CURLOPT_PROXYPORT, $proxy_port);
  82. }
  83. /**
  84. * 默认跳过https证书验证
  85. * @param bool $ignore
  86. */
  87. public function sslVerify($ignore = false)
  88. {
  89. if ($ignore) {
  90. curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, TRUE);//严格校验
  91. curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2);//严格校验
  92. } else {
  93. curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);//跳过证书检查
  94. curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);//从证书中检查SSL加密算法是否存在
  95. }
  96. }
  97. /**
  98. * 设置证书,默认不需要
  99. * @param $sslCertPath string
  100. * @param $sslKeyPath string
  101. */
  102. public function useCert($sslCertPath, $sslKeyPath)
  103. {
  104. //设置证书,证书路径,注意应该填写绝对路径
  105. //使用证书:cert 与 key 分别属于两个.pem文件
  106. //证书文件请放入服务器的非web目录下,防止被他人下载
  107. curl_setopt($this->curl, CURLOPT_SSLCERTTYPE, 'PEM');
  108. curl_setopt($this->curl, CURLOPT_SSLCERT, $sslCertPath);
  109. curl_setopt($this->curl, CURLOPT_SSLKEYTYPE, 'PEM');
  110. curl_setopt($this->curl, CURLOPT_SSLKEY, $sslKeyPath);
  111. }
  112. /**
  113. * @param $referer string
  114. */
  115. public function setReferer($referer)
  116. {
  117. if ($referer) {
  118. curl_setopt($this->curl, CURLOPT_REFERER, $referer);
  119. }
  120. }
  121. /**
  122. * 设置转码
  123. * @param string $encode 默认是可解压缩gzip、deflate等压缩文件
  124. */
  125. public function setEncoding($encode = '')
  126. {
  127. curl_setopt($this->curl, CURLOPT_ENCODING, $encode);
  128. }
  129. /**
  130. * @param $header array
  131. */
  132. public function setHeader($header)
  133. {
  134. if (!$header) {
  135. return;
  136. }
  137. // 将索引数组转为键值数组
  138. // 例如:['User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64)'] 转为 ['User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64)']
  139. if (!isset($header[0])) {
  140. foreach ($header as $hk => $hv) {
  141. unset ($header [$hk]);
  142. $header [] = $hk . ':' . $hv;
  143. }
  144. }
  145. curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header);
  146. }
  147. /**
  148. * @param $cookie string
  149. */
  150. public function setCookie($cookie)
  151. {
  152. if ($cookie) {
  153. curl_setopt($this->curl, CURLOPT_COOKIE, $cookie);
  154. }
  155. }
  156. /**
  157. * 获取响应头
  158. */
  159. public function showResponseHeader()
  160. {
  161. curl_setopt($this->curl, CURLOPT_HEADER, true);//显示响应内容+响应头
  162. $this->showResponseHeader = true;
  163. }
  164. /**
  165. * 获取请求头
  166. */
  167. public function showRequestHeader()
  168. {
  169. curl_setopt($this->curl, CURLINFO_HEADER_OUT, true);
  170. $this->showRequestHeader = true;
  171. }
  172. //执行请求
  173. public function exec()
  174. {
  175. // 发送请求
  176. $this->response = curl_exec($this->curl);
  177. $this->httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
  178. if ($error = curl_error($this->curl)) {
  179. $this->error = $error;
  180. } else {
  181. if ($this->showRequestHeader) {// 请求头
  182. $this->requestHeader = curl_getinfo($this->curl, CURLINFO_HEADER_OUT);
  183. }
  184. //头信息大小,curl 在这里有 BUG,需要升级到7.4 或 7.5
  185. $headerSize = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
  186. $this->responseBody = substr($this->response, $headerSize);
  187. $this->responseHeader = substr($this->response, 0, $headerSize);
  188. }
  189. curl_close($this->curl);//结束
  190. }
  191. //响应状态码
  192. public function getHttpCode()
  193. {
  194. return $this->httpCode;
  195. }
  196. //响应内容
  197. public function getResponse()
  198. {
  199. if ($this->showResponseHeader) {
  200. return $this->responseBody;
  201. }
  202. return $this->response;
  203. }
  204. //响应的错误消息
  205. public function getError()
  206. {
  207. return $this->error;
  208. }
  209. //响应体
  210. public function getResponseBody()
  211. {
  212. return $this->responseBody;
  213. }
  214. //请求头
  215. public function getRequestHeader()
  216. {
  217. return $this->showRequestHeader ? $this->requestHeader : '';
  218. }
  219. //响应头
  220. public function getResponseHeader()
  221. {
  222. return $this->showResponseHeader ? $this->responseHeader : '';
  223. }
  224. }