Files
glade_cameras/components/ApiClient.php
T

57 lines
1.2 KiB
PHP

<?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');
}
public function getAllEvents($id)
{
return $this->sendRequest('events/index/MonitorId:'.$id.'.json');
}
public function getEvents($id, $start_time, $page = 1)
{
return $this->sendRequest('events/index/MonitorId:'.$id.'/'.
urlencode('StartTime >=:'.$start_time).'.json?page='.$page);
}
}