igfs/kernel/helpers/RESTClient.php

52 lines
1.3 KiB
PHP
Raw Normal View History

2024-11-18 16:04:57 +03:00
<?php
namespace kernel\helpers;
2024-11-19 13:43:13 +03:00
use GuzzleHttp\Exception\GuzzleException;
2024-11-18 16:04:57 +03:00
use http\Client;
class RESTClient
{
2024-11-19 13:43:13 +03:00
/**
* @throws GuzzleException
*/
public static function request(string $url, string $method = 'GET'): \Psr\Http\Message\ResponseInterface
2024-11-18 16:04:57 +03:00
{
$client = new \GuzzleHttp\Client();
return $client->request($method, $url, [
'headers' => [
'Authorization' => 'Bearer ' . $_ENV['MODULE_SHOP_TOKEN']
]
]);
}
2024-12-16 14:26:13 +03:00
/**
* @throws GuzzleException
*/
public static function request_without_auth(string $url, string $method = 'GET'): \Psr\Http\Message\ResponseInterface
{
$client = new \GuzzleHttp\Client();
return $client->request($method, $url);
}
/**
* @throws GuzzleException
*/
public static function post(string $url, array $data = [], bool $auth = true): \Psr\Http\Message\ResponseInterface
{
$headers = [];
if ($auth){
$headers = [
'Authorization' => 'Bearer ' . $_ENV['MODULE_SHOP_TOKEN']
];
}
$client = new \GuzzleHttp\Client();
return $client->request("POST", $url, [
'form_params' => $data,
'headers' => $headers,
]);
}
2024-11-18 16:04:57 +03:00
}