downgrade to kirby v3
This commit is contained in:
@@ -2,13 +2,11 @@
|
||||
|
||||
namespace Kirby\Http;
|
||||
|
||||
use CurlHandle;
|
||||
use Exception;
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Filesystem\F;
|
||||
use Kirby\Toolkit\Str;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* A handy little class to handle
|
||||
@@ -25,7 +23,10 @@ class Remote
|
||||
public const CA_INTERNAL = 1;
|
||||
public const CA_SYSTEM = 2;
|
||||
|
||||
public static array $defaults = [
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $defaults = [
|
||||
'agent' => null,
|
||||
'basicAuth' => null,
|
||||
'body' => true,
|
||||
@@ -40,17 +41,64 @@ class Remote
|
||||
'timeout' => 10,
|
||||
];
|
||||
|
||||
public string|null $content = null;
|
||||
public CurlHandle|false $curl;
|
||||
public array $curlopt = [];
|
||||
public int $errorCode;
|
||||
public string $errorMessage;
|
||||
public array $headers = [];
|
||||
public array $info = [];
|
||||
public array $options = [];
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $content;
|
||||
|
||||
/**
|
||||
* @throws \Exception when the curl request failed
|
||||
* @var resource
|
||||
*/
|
||||
public $curl;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $curlopt = [];
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $errorCode;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $errorMessage;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $headers = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $info = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $options = [];
|
||||
|
||||
/**
|
||||
* Magic getter for request info data
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call(string $method, array $arguments = [])
|
||||
{
|
||||
$method = str_replace('-', '_', Str::kebab($method));
|
||||
return $this->info[$method] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct(string $url, array $options = [])
|
||||
{
|
||||
@@ -59,15 +107,14 @@ class Remote
|
||||
// use the system CA store by default if
|
||||
// one has been configured in php.ini
|
||||
$cainfo = ini_get('curl.cainfo');
|
||||
|
||||
// Suppress warnings e.g. if system CA is outside of open_basedir (See: issue #6236)
|
||||
if (empty($cainfo) === false && @is_file($cainfo) === true) {
|
||||
if (empty($cainfo) === false && is_file($cainfo) === true) {
|
||||
$defaults['ca'] = self::CA_SYSTEM;
|
||||
}
|
||||
|
||||
// update the defaults with App config if set;
|
||||
// request the App instance lazily
|
||||
if ($app = App::instance(null, true)) {
|
||||
$app = App::instance(null, true);
|
||||
if ($app !== null) {
|
||||
$defaults = array_merge($defaults, $app->option('remote', []));
|
||||
}
|
||||
|
||||
@@ -81,40 +128,27 @@ class Remote
|
||||
$this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic getter for request info data
|
||||
*/
|
||||
public function __call(string $method, array $arguments = [])
|
||||
public static function __callStatic(string $method, array $arguments = [])
|
||||
{
|
||||
$method = str_replace('-', '_', Str::kebab($method));
|
||||
return $this->info[$method] ?? null;
|
||||
}
|
||||
|
||||
public static function __callStatic(
|
||||
string $method,
|
||||
array $arguments = []
|
||||
): static {
|
||||
return new static(
|
||||
url: $arguments[0],
|
||||
options: array_merge(
|
||||
['method' => strtoupper($method)],
|
||||
$arguments[1] ?? []
|
||||
)
|
||||
);
|
||||
return new static($arguments[0], array_merge(['method' => strtoupper($method)], $arguments[1] ?? []));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the http status code
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function code(): int|null
|
||||
public function code(): ?int
|
||||
{
|
||||
return $this->info['http_code'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the response content
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function content(): string|null
|
||||
public function content()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
@@ -123,9 +157,8 @@ class Remote
|
||||
* Sets up all curl options and sends the request
|
||||
*
|
||||
* @return $this
|
||||
* @throws \Exception when the curl request failed
|
||||
*/
|
||||
public function fetch(): static
|
||||
public function fetch()
|
||||
{
|
||||
// curl options
|
||||
$this->curlopt = [
|
||||
@@ -138,7 +171,7 @@ class Remote
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_HEADER => false,
|
||||
CURLOPT_HEADERFUNCTION => function ($curl, $header): int {
|
||||
CURLOPT_HEADERFUNCTION => function ($curl, $header) {
|
||||
$parts = Str::split($header, ':');
|
||||
|
||||
if (empty($parts[0]) === false && empty($parts[1]) === false) {
|
||||
@@ -186,10 +219,10 @@ class Remote
|
||||
$headers = [];
|
||||
foreach ($this->options['headers'] as $key => $value) {
|
||||
if (is_string($key) === true) {
|
||||
$value = $key . ': ' . $value;
|
||||
$headers[] = $key . ': ' . $value;
|
||||
} else {
|
||||
$headers[] = $value;
|
||||
}
|
||||
|
||||
$headers[] = $value;
|
||||
}
|
||||
|
||||
$this->curlopt[CURLOPT_HTTPHEADER] = $headers;
|
||||
@@ -263,9 +296,11 @@ class Remote
|
||||
/**
|
||||
* Static method to send a GET request
|
||||
*
|
||||
* @throws \Exception when the curl request failed
|
||||
* @param string $url
|
||||
* @param array $params
|
||||
* @return static
|
||||
*/
|
||||
public static function get(string $url, array $params = []): static
|
||||
public static function get(string $url, array $params = [])
|
||||
{
|
||||
$defaults = [
|
||||
'method' => 'GET',
|
||||
@@ -276,10 +311,7 @@ class Remote
|
||||
$query = http_build_query($options['data']);
|
||||
|
||||
if (empty($query) === false) {
|
||||
$url = match (Url::hasQuery($url)) {
|
||||
true => $url . '&' . $query,
|
||||
default => $url . '?' . $query
|
||||
};
|
||||
$url = Url::hasQuery($url) === true ? $url . '&' . $query : $url . '?' . $query;
|
||||
}
|
||||
|
||||
// remove the data array from the options
|
||||
@@ -290,6 +322,8 @@ class Remote
|
||||
|
||||
/**
|
||||
* Returns all received headers
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function headers(): array
|
||||
{
|
||||
@@ -298,6 +332,8 @@ class Remote
|
||||
|
||||
/**
|
||||
* Returns the request info
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function info(): array
|
||||
{
|
||||
@@ -308,15 +344,17 @@ class Remote
|
||||
* Decode the response content
|
||||
*
|
||||
* @param bool $array decode as array or object
|
||||
* @psalm-return ($array is true ? array|null : stdClass|null)
|
||||
* @return array|\stdClass
|
||||
*/
|
||||
public function json(bool $array = true): array|stdClass|null
|
||||
public function json(bool $array = true)
|
||||
{
|
||||
return json_decode($this->content(), $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function method(): string
|
||||
{
|
||||
@@ -326,6 +364,8 @@ class Remote
|
||||
/**
|
||||
* Returns all options which have been
|
||||
* set for the current request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function options(): array
|
||||
{
|
||||
@@ -334,28 +374,35 @@ class Remote
|
||||
|
||||
/**
|
||||
* Internal method to handle post field data
|
||||
*
|
||||
* @param mixed $data
|
||||
* @return mixed
|
||||
*/
|
||||
protected function postfields($data)
|
||||
{
|
||||
if (is_object($data) || is_array($data)) {
|
||||
return http_build_query($data);
|
||||
} else {
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static method to init this class and send a request
|
||||
*
|
||||
* @throws \Exception when the curl request failed
|
||||
* @param string $url
|
||||
* @param array $params
|
||||
* @return static
|
||||
*/
|
||||
public static function request(string $url, array $params = []): static
|
||||
public static function request(string $url, array $params = [])
|
||||
{
|
||||
return new static($url, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the request Url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function url(): string
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user