From ae4df855d0e6acc7dcada35e27f1b82de77a4e33 Mon Sep 17 00:00:00 2001 From: codenoid <14269809+codenoid@users.noreply.github.com> Date: Mon, 19 Aug 2024 11:47:53 +0700 Subject: [PATCH] use curl --- src/LokalSo/Lokal.php | 87 +++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 33 deletions(-) diff --git a/src/LokalSo/Lokal.php b/src/LokalSo/Lokal.php index ddd932a..2c77265 100644 --- a/src/LokalSo/Lokal.php +++ b/src/LokalSo/Lokal.php @@ -5,7 +5,6 @@ use Exception; use JsonSerializable; - use GuzzleHttp\Client; const LOKAL_SERVER_MIN_VERSION = "v0.6.0"; const LOKAL_SO_BANNER = <<base_url = $base_url; - $this->client = new Client([ - 'base_uri' => $this->base_url, - 'headers' => [ - 'User-Agent' => 'Lokal PHP - github.com/lokal-so/lokal-php', - 'Content-Type' => 'application/json', - ], - ]); } public function setBaseUrl(string $base_url): Lokal { $this->base_url = $base_url; - $this->client = new Client([ - 'base_uri' => $this->base_url, - 'headers' => [ - 'User-Agent' => 'Lokal PHP - github.com/lokal-so/lokal-php', - 'Content-Type' => 'application/json', - ], - ]); return $this; } @@ -342,29 +322,70 @@ private static function outJson(string $data) public function postJson(string $path, string $body): array { - $response = $this->client->post($path, ['body' => $body]); - $this->checkHeaders($response->getHeaders()); - - return self::outJson($response->getBody()->getContents()); + $hdr = ["Content-Type: application/json"]; + $opt = [CURLOPT_POSTFIELDS => $body]; + return self::outJson($this->curl("POST", $path, $opt, $hdr)); } public function getJson(string $path): array { - $response = $this->client->get($path); - $this->checkHeaders($response->getHeaders()); + return self::outJson($this->curl("GET", $path)); + } + + private static function curlHeaderCheck(string $hdr): void + { + if (strpos($hdr, ":") === false) + return; + + list($key, $val) = explode(":", $hdr, 2); - return self::outJson($response->getBody()->getContents()); + $key = strtolower(trim($key)); + $val = trim($val); + + if ($key !== "lokal-server-version") + return; + + if (version_compare($val, substr(LOKAL_SERVER_MIN_VERSION, 1), "<")) { + $err = sprintf("Outdated software version, server version: %s, server version required (minimal): %s", $val, LOKAL_SERVER_MIN_VERSION); + throw new Exception($err); + } } - private function checkHeaders(array $headers): void + public function curl(string $method, string $path, array $opt = [], array $hdr = []): string { - if (isset($headers['lokal-server-version'])) { - $serverVersion = $headers['lokal-server-version'][0]; - if (version_compare($serverVersion, substr(LOKAL_SERVER_MIN_VERSION, 1), "<")) { - $err = sprintf("Outdated software version, server version: %s, server version required (minimal): %s", $serverVersion, LOKAL_SERVER_MIN_VERSION); - throw new Exception($err); + $hdr_chk_func = function ($ch, $hdr) { + self::curlHeaderCheck($hdr); + return strlen($hdr); + }; + + $ch = curl_init(); + $default_opts = [ + CURLOPT_URL => $this->base_url . $path, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HEADER => false, + CURLOPT_USERAGENT => "Lokal PHP - github.com/lokal-so/lokal-php", + CURLOPT_HTTPHEADER => $hdr, + CURLOPT_CUSTOMREQUEST => $method, + CURLOPT_HEADERFUNCTION => $hdr_chk_func + ]; + + foreach ($opt as $key => $value) + $default_opts[$key] = $value; + + curl_setopt_array($ch, $default_opts); + $res = curl_exec($ch); + $err = curl_error($ch); + $ern = curl_errno($ch); + curl_close($ch); + + if ($ern !== 0) { + if (str_contains($err, "Couldn't connect to server")) { + throw new Exception("Curl error ({$ern}): {$err} - You may need to install Lokal Client at Lokal.so/download"); } + throw new Exception("Curl error ({$ern}): {$err}"); } + + return $res; } }; } /* namespace LokalSo */