mirror of
https://codeberg.org/reflect/reflect-client-php.git
synced 2025-09-13 17:43:42 +02:00
fix: PHP 8.2 deprecations warnings (#6)
This commit is contained in:
parent
cde845d61b
commit
47cee961d1
1 changed files with 28 additions and 16 deletions
|
@ -19,25 +19,37 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
|
// API key string
|
||||||
|
private ?string $key;
|
||||||
|
// Connection method
|
||||||
|
private Connection $con;
|
||||||
|
// Request endpoitn string
|
||||||
|
private string $endpoint;
|
||||||
|
|
||||||
|
// Socket instance
|
||||||
|
private Socket|false $socket = false;
|
||||||
|
// Flag: Allow unverified SSL certificates for HTTPS
|
||||||
|
private bool $https_peer_verify = true;
|
||||||
|
|
||||||
// Use this HTTP method if no method specified to call()
|
// Use this HTTP method if no method specified to call()
|
||||||
const HTTP_DEFAULT_METHOD = Method::GET;
|
const HTTP_DEFAULT_METHOD = Method::GET;
|
||||||
// The amount of bytes to read for each chunk from socket
|
// The amount of bytes to read for each chunk from socket
|
||||||
const SOCKET_READ_BYTES = 2048;
|
const SOCKET_READ_BYTES = 2048;
|
||||||
|
|
||||||
public function __construct(string $endpoint, string $key = null, Connection $con = null, bool $https_peer_verify = true) {
|
public function __construct(string $endpoint, string $key = null, Connection $con = null, bool $https_peer_verify = true) {
|
||||||
$this->_con = $con ?: $this::resolve_connection($endpoint);
|
$this->con = $con ?: $this::resolve_connection($endpoint);
|
||||||
$this->_endpoint = $endpoint;
|
$this->endpoint = $endpoint;
|
||||||
$this->_key = $key;
|
$this->key = $key;
|
||||||
|
|
||||||
if ($this->_con === Connection::AF_UNIX) {
|
if ($this->con === Connection::AF_UNIX) {
|
||||||
// Connect to Reflect UNIX socket
|
// Connect to Reflect UNIX socket
|
||||||
$this->_socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
|
$this->_socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
|
||||||
$conn = socket_connect($this->_socket, $this->_endpoint);
|
$conn = socket_connect($this->_socket, $this->endpoint);
|
||||||
} else if ($this->_con === Connection::HTTP) {
|
} else if ($this->con === Connection::HTTP) {
|
||||||
// Append tailing "/" for HTTP if absent
|
// Append tailing "/" for HTTP if absent
|
||||||
$this->_endpoint = substr($this->_endpoint, -1) === "/" ? $this->_endpoint : $this->_endpoint . "/";
|
$this->endpoint = substr($this->endpoint, -1) === "/" ? $this->endpoint : $this->endpoint . "/";
|
||||||
// Flag which enables or disables SSL peer validation (for self-signed certificates)
|
// Flag which enables or disables SSL peer validation (for self-signed certificates)
|
||||||
$this->_https_peer_verify = $https_peer_verify;
|
$this->https_peer_verify = $https_peer_verify;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,8 +73,8 @@
|
||||||
$headers = ["Content-Type: application/json"];
|
$headers = ["Content-Type: application/json"];
|
||||||
|
|
||||||
// Append Authentication header if API key is provided
|
// Append Authentication header if API key is provided
|
||||||
if (!empty($this->_key)) {
|
if (!empty($this->key)) {
|
||||||
$headers[] = "Authorization: Bearer {$this->_key}";
|
$headers[] = "Authorization: Bearer {$this->key}";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append new line chars to each header
|
// Append new line chars to each header
|
||||||
|
@ -74,7 +86,7 @@
|
||||||
private function http_call(string $endpoint, Method|string $method = (__CLASS__)::HTTP_DEFAULT_METHOD, array $payload = null): array {
|
private function http_call(string $endpoint, Method|string $method = (__CLASS__)::HTTP_DEFAULT_METHOD, array $payload = null): array {
|
||||||
// Resolve string to enum
|
// Resolve string to enum
|
||||||
$method = $this::resolve_method($method);
|
$method = $this::resolve_method($method);
|
||||||
// Remove leading "/" if present, as it's already present in $this->_endpoint
|
// Remove leading "/" if present, as it's already present in $this->endpoint
|
||||||
$endpoint = substr($endpoint, 0, 1) !== "/" ? $endpoint : substr($endpoint, 1, strlen($endpoint) - 1);
|
$endpoint = substr($endpoint, 0, 1) !== "/" ? $endpoint : substr($endpoint, 1, strlen($endpoint) - 1);
|
||||||
|
|
||||||
$data = stream_context_create([
|
$data = stream_context_create([
|
||||||
|
@ -85,13 +97,13 @@
|
||||||
"content" => !empty($payload) ? json_encode($payload) : ""
|
"content" => !empty($payload) ? json_encode($payload) : ""
|
||||||
],
|
],
|
||||||
"ssl" => [
|
"ssl" => [
|
||||||
"verify_peer" => $this->_https_peer_verify,
|
"verify_peer" => $this->https_peer_verify,
|
||||||
"verify_peer_name" => $this->_https_peer_verify,
|
"verify_peer_name" => $this->https_peer_verify,
|
||||||
"allow_self_signed" => !$this->_https_peer_verify
|
"allow_self_signed" => !$this->https_peer_verify
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$resp = file_get_contents($this->_endpoint . $endpoint, false, $data);
|
$resp = file_get_contents($this->endpoint . $endpoint, false, $data);
|
||||||
|
|
||||||
// Get HTTP response code from $http_response_header which materializes out of thin air after file_get_contents().
|
// Get HTTP response code from $http_response_header which materializes out of thin air after file_get_contents().
|
||||||
// The first header line and second word will contain the status code.
|
// The first header line and second word will contain the status code.
|
||||||
|
@ -119,7 +131,7 @@
|
||||||
$method = $this::resolve_method($method);
|
$method = $this::resolve_method($method);
|
||||||
|
|
||||||
// Call endpoint over UNIX socket
|
// Call endpoint over UNIX socket
|
||||||
if ($this->_con === Connection::AF_UNIX) {
|
if ($this->con === Connection::AF_UNIX) {
|
||||||
// Return response as assoc array
|
// Return response as assoc array
|
||||||
return json_decode($this->socket_txn(
|
return json_decode($this->socket_txn(
|
||||||
// Send request as stringified JSON
|
// Send request as stringified JSON
|
||||||
|
|
Loading…
Add table
Reference in a new issue