helper_curl.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 中闽 < 1464674022@qq.com >
  5. * Date: 2019/12/5
  6. * Time: 17:44
  7. */
  8. // +----------------------------------------------------------------------
  9. // curl方法
  10. // +----------------------------------------------------------------------
  11. /**
  12. * get 请求
  13. * @param $url
  14. * @param array $params
  15. * @param string $cookie
  16. * @return array
  17. */
  18. function get_url($url, $params = [], $cookie = "")
  19. {
  20. if ($params) {
  21. $url = $url . '?' . http_build_query($params);
  22. }
  23. $curl = new \network\CurlHelper($url);
  24. $curl->setCookie($cookie);
  25. $curl->exec();
  26. return [$curl->getResponse(), $curl->getHttpCode(), $curl->getError()];
  27. }
  28. /**
  29. * post 请求
  30. * @param $url
  31. * @param array $params
  32. * @param array $header
  33. * @param string $cookie
  34. * @return array
  35. */
  36. function post_url($url, $params = [], $header = [], $cookie = "")
  37. {
  38. $curl = new \network\CurlHelper($url, $params);
  39. $curl->setHeader($header);
  40. $curl->setCookie($cookie);
  41. $curl->exec();
  42. return [$curl->getResponse(), $curl->getHttpCode(), $curl->getError()];
  43. }
  44. /**
  45. * 提交json格式的请求
  46. * @param $url
  47. * @param array $params
  48. * @return array
  49. */
  50. function post_json($url, $params = [])
  51. {
  52. $header = ['Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen(json_encode($params))];
  53. $curl = new \network\CurlHelper($url, json_encode($params));
  54. $curl->setHeader($header);
  55. $curl->exec();
  56. return [$curl->getResponse(), $curl->getHttpCode(), $curl->getError()];
  57. }
  58. /**
  59. * 有的接口会通过cookie防采集
  60. * @param $url
  61. * @param $expire int 默认缓存一小时
  62. * @return mixed
  63. * @throws Exception
  64. */
  65. function get_by_set_cookie($url, $expire = 3600)
  66. {
  67. $cookieName = "cache-setcookie-key";
  68. $cookieVal = isset($_COOKIE[$cookieName]) ? $_COOKIE[$cookieName] : "";
  69. list($response, $httpCode) = get_url($url, [], $cookieVal);
  70. if ($httpCode == 302) {
  71. $curl = new \network\CurlHelper($url);
  72. $curl->showResponseHeader();//输出响应头
  73. $curl->exec();
  74. $response = $curl->getResponse();
  75. // 正则获取set-cookie
  76. if (preg_match_all('/Set-Cookie: (.*?);/m', $response, $cookie)) {
  77. $realCookie = implode(';', $cookie['1']);
  78. setcookie($cookieName, $realCookie, time() + $expire);//缓存
  79. list($response) = get_url($url, [], $cookieVal);
  80. } else {
  81. throw new Exception("获取Set-Cookie失败");
  82. }
  83. } else if ($httpCode == 301) {
  84. throw new Exception("获取失败,返回301跳转");
  85. } else if ($httpCode != 200) {
  86. throw new Exception("获取失败,httpCode = $httpCode");
  87. }
  88. return $response;
  89. }