2019-08-07 05:48:00 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Support;
|
|
|
|
|
2019-08-09 02:52:34 +08:00
|
|
|
use GuzzleHttp\Client as GuzzleClient;
|
2021-06-04 22:51:59 +08:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2019-08-07 05:48:00 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper for HTTP stuff
|
|
|
|
*/
|
|
|
|
class HttpClient
|
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
private GuzzleClient $httpClient;
|
2019-08-07 05:48:00 +08:00
|
|
|
|
2021-06-04 22:51:59 +08:00
|
|
|
public function __construct(GuzzleClient $httpClient)
|
|
|
|
{
|
2019-08-07 05:48:00 +08:00
|
|
|
$this->httpClient = $httpClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download a URI. If a file is given, it will save the downloaded
|
|
|
|
* content into that file
|
|
|
|
*
|
|
|
|
* @param $uri
|
|
|
|
* @param array $opts
|
|
|
|
*
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get($uri, array $opts = [])
|
|
|
|
{
|
|
|
|
$opts = array_merge([
|
|
|
|
'connect_timeout' => 2, // wait two seconds by default
|
|
|
|
], $opts);
|
|
|
|
|
|
|
|
$response = $this->httpClient->request('GET', $uri, $opts);
|
|
|
|
|
|
|
|
$body = $response->getBody()->getContents();
|
|
|
|
$content_type = $response->getHeaderLine('content-type');
|
|
|
|
if (strpos($content_type, 'application/json') !== false) {
|
|
|
|
$body = \GuzzleHttp\json_decode($body, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $body;
|
|
|
|
}
|
2020-02-11 06:47:46 +08:00
|
|
|
|
2021-06-04 22:51:59 +08:00
|
|
|
/**
|
|
|
|
* @param $uri
|
|
|
|
* @param $body
|
|
|
|
* @param array $opts
|
|
|
|
*
|
2022-03-14 23:45:18 +08:00
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*
|
2021-06-04 22:51:59 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2022-03-14 23:45:18 +08:00
|
|
|
public function post($uri, $body, array $opts = []): mixed
|
2021-06-04 22:51:59 +08:00
|
|
|
{
|
|
|
|
$opts = array_merge([
|
|
|
|
'connect_timeout' => 2,
|
|
|
|
RequestOptions::JSON => $body,
|
|
|
|
], $opts);
|
|
|
|
|
|
|
|
$response = $this->httpClient->post($uri, $opts);
|
|
|
|
$content_type = $response->getHeaderLine('content-type');
|
2022-03-14 23:45:18 +08:00
|
|
|
$content = $response->getBody()->getContents();
|
|
|
|
|
|
|
|
if (str_contains($content_type, 'application/json') !== false) {
|
|
|
|
$content = \GuzzleHttp\json_decode($content, true);
|
2021-06-04 22:51:59 +08:00
|
|
|
}
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $uri
|
|
|
|
* @param $body
|
|
|
|
* @param array $opts
|
|
|
|
*
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function form_post($uri, $body, array $opts = []): mixed
|
|
|
|
{
|
|
|
|
$opts = array_merge([
|
|
|
|
'connect_timeout' => 2,
|
|
|
|
'form_params' => $body,
|
|
|
|
], $opts);
|
|
|
|
|
|
|
|
$response = $this->httpClient->request('POST', $uri, $opts);
|
|
|
|
$content_type = $response->getHeaderLine('content-type');
|
|
|
|
$content = $response->getBody()->getContents();
|
|
|
|
|
|
|
|
if (str_contains($content_type, 'application/json') !== false) {
|
|
|
|
$content = \GuzzleHttp\json_decode($content, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
2021-06-04 22:51:59 +08:00
|
|
|
}
|
|
|
|
|
2020-02-11 06:47:46 +08:00
|
|
|
/**
|
|
|
|
* Download a file to a given path
|
|
|
|
*
|
|
|
|
* @param $uri
|
|
|
|
* @param $local_path
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function download($uri, $local_path)
|
|
|
|
{
|
2020-02-12 01:32:41 +08:00
|
|
|
$opts = [];
|
|
|
|
if ($local_path !== null) {
|
|
|
|
$opts['sink'] = $local_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->httpClient->request('GET', $uri, $opts);
|
2020-02-11 06:47:46 +08:00
|
|
|
|
2020-02-12 01:32:41 +08:00
|
|
|
$body = $response->getBody()->getContents();
|
|
|
|
if ($response->getHeader('content-type') === 'application/json') {
|
|
|
|
$body = \GuzzleHttp\json_decode($body);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $body;
|
2020-02-11 06:47:46 +08:00
|
|
|
}
|
2019-08-07 05:48:00 +08:00
|
|
|
}
|