BaseClient.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace crmeb\services\upload;
  14. /**
  15. * 基础请求
  16. * Class BaseClient
  17. * @author 等风来
  18. * @email 136327134@qq.com
  19. * @date 2023/5/18
  20. * @package crmeb\services\upload
  21. */
  22. abstract class BaseClient
  23. {
  24. /**
  25. * 是否解析为xml
  26. * @var bool
  27. */
  28. protected $isXml = true;
  29. /**
  30. *
  31. * @var []callable
  32. */
  33. protected $curlFn = [];
  34. /**
  35. * @param callable $curlFn
  36. * @return $this
  37. * @author 等风来
  38. * @email 136327134@qq.com
  39. * @date 2023/5/18
  40. */
  41. public function middleware(callable $curlFn)
  42. {
  43. $this->curlFn[] = $curlFn;
  44. return $this;
  45. }
  46. /**
  47. * 发起请求
  48. * @param string $url
  49. * @param string $method
  50. * @param array $data
  51. * @param array $clientHeader
  52. * @param int $timeout
  53. * @return array|bool|SimpleXMLElement|mixed
  54. */
  55. protected function requestClient(string $url, string $method, array $data = [], array $clientHeader = [], int $timeout = 10)
  56. {
  57. $headers = [];
  58. foreach ($clientHeader as $key => $item) {
  59. $headers[] = $key . ':' . $item;
  60. }
  61. $curl = curl_init($url);
  62. //请求方式
  63. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  64. //post请求
  65. if (!empty($data['body'])) {
  66. curl_setopt($curl, CURLOPT_POSTFIELDS, $data['body']);
  67. } else if (!empty($data['json'])) {
  68. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data['json']));
  69. } else {
  70. $curlFn = $this->curlFn;
  71. foreach ($curlFn as $item) {
  72. if ($item instanceof \Closure) {
  73. $curlFn($curl);
  74. }
  75. }
  76. }
  77. //超时时间
  78. curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
  79. //设置header头
  80. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  81. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  82. //返回抓取数据
  83. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  84. //输出header头信息
  85. curl_setopt($curl, CURLOPT_HEADER, true);
  86. //TRUE 时追踪句柄的请求字符串,从 PHP 5.1.3 开始可用。这个很关键,就是允许你查看请求header
  87. curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  88. //https请求
  89. if (1 == strpos("$" . $url, "https://")) {
  90. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  91. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  92. }
  93. [$content, $status] = [curl_exec($curl), curl_getinfo($curl)];
  94. curl_close($curl);
  95. $content = trim(substr($content, $status['header_size']));
  96. if ($content) {
  97. if ($this->isXml) {
  98. return XML::parse($content);
  99. } else {
  100. return json_decode($content, true);
  101. }
  102. } else {
  103. return true;
  104. }
  105. }
  106. }