From 196dfbcbda9bcece81ab6869da3a32593e1798f7 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Thu, 30 Apr 2020 15:23:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ehttp=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/library/Http.php | 84 +++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 extend/catcher/library/Http.php diff --git a/extend/catcher/library/Http.php b/extend/catcher/library/Http.php new file mode 100644 index 0000000..0ed2365 --- /dev/null +++ b/extend/catcher/library/Http.php @@ -0,0 +1,84 @@ + 5, // 请求超时时间 + 'on_headers' => function (ResponseInterface $response) { + $response->getHeader('Content-Length'); + }, + 'on_stats' => function (TransferStats $stats) { + }, + ]; + + if (!empty($this->auth)) { + $params['auth'] = $this->auth; + } + + if (!empty($proxy)) { + $params['proxy'] = $this->proxy; + } + + if ($filePath) { + $resource = fopen($filePath, 'w+'); + + $stream = stream_for($resource); + + $params['save_to'] = $stream; + } + + return (new Client())->request('get', $remoteUrl, $params); + } catch (\Exception $e) { + throw new FailedException($e->getMessage()); + } + + return $filePath; + } + + /** + * 认证 + * + * @time 2020年04月30日 + * @param $username + * @param $password + * @return $this + */ + public function auth($username, $password) + { + $this->auth = [$username, $password]; + + return $this; + } + + + public function proxy(array $proxy) + { + $this->proxy = $proxy; + + return $this; + } +}