CURL extension and api test command
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user