CURL extension and api test command

This commit is contained in:
ftc
2023-05-18 22:15:55 +03:00
parent 696600086f
commit 4232800cb9
39 changed files with 13179 additions and 25 deletions
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace app\components;
use linslin\yii2\curl;
use Yii;
class ApiClient
{
private $url;
private $http_client;
public function __construct($url)
{
$this->url = $url;
$this->http_client = new curl\Curl();
}
private function sendRequest($path, $json = null, $method = 'get')
{
$url = $this->url . '/' . $path;
$this->http_client->reset();
$this->http_client->setOption(CURLOPT_TIMEOUT, 3600);
$this->http_client->setOption(CURLOPT_HTTPHEADER,
['Content-Type: application/json',
]);
if (!empty($json))
{
$this->http_client->setOption(CURLOPT_POSTFIELDS, $json);
}
$result = $this->http_client->$method($url);
if ($this->http_client->responseCode !== 200)
{
throw new \Exception("Call to ZM failed: " . $this->http_client->responseCode . "\n" . $result);
}
return $result;
}
public function getVersion()
{
return $this->sendRequest('host/getVersion.json');
}
}