123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace crmeb\services\upload;
- abstract class BaseClient
- {
-
- protected $isXml = true;
-
- protected $curlFn = [];
-
- public function middleware(callable $curlFn)
- {
- $this->curlFn[] = $curlFn;
- return $this;
- }
-
- protected function requestClient(string $url, string $method, array $data = [], array $clientHeader = [], int $timeout = 10)
- {
- $headers = [];
- foreach ($clientHeader as $key => $item) {
- $headers[] = $key . ':' . $item;
- }
- $curl = curl_init($url);
-
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
-
- if (!empty($data['body'])) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data['body']);
- } else if (!empty($data['json'])) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data['json']));
- } else {
- $curlFn = $this->curlFn;
- foreach ($curlFn as $item) {
- if ($item instanceof \Closure) {
- $curlFn($curl);
- }
- }
- }
-
- curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
-
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_FAILONERROR, false);
-
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
-
- curl_setopt($curl, CURLOPT_HEADER, true);
-
- curl_setopt($curl, CURLINFO_HEADER_OUT, true);
-
- if (1 == strpos("$" . $url, "https://")) {
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- [$content, $status] = [curl_exec($curl), curl_getinfo($curl)];
- curl_close($curl);
- $content = trim(substr($content, $status['header_size']));
- if ($content) {
- if ($this->isXml) {
- return XML::parse($content);
- } else {
- return json_decode($content, true);
- }
- } else {
- return true;
- }
- }
- }
|