Skip to content

Commit

Permalink
use curl
Browse files Browse the repository at this point in the history
  • Loading branch information
codenoid committed Aug 19, 2024
1 parent c643eff commit ae4df85
Showing 1 changed file with 54 additions and 33 deletions.
87 changes: 54 additions & 33 deletions src/LokalSo/Lokal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Exception;
use JsonSerializable;
use GuzzleHttp\Client;

const LOKAL_SERVER_MIN_VERSION = "v0.6.0";
const LOKAL_SO_BANNER = <<<BANNER
Expand Down Expand Up @@ -295,33 +294,14 @@ class Lokal
*/
private string $base_url;

/**
* @var \GuzzleHttp\Client
*/
private Client $client;

public function __construct(string $base_url = "http://127.0.0.1:6174")
{
$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',
],
]);
}

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;
}

Expand All @@ -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 */

0 comments on commit ae4df85

Please sign in to comment.