init commit
This commit is contained in:
153
class_livesklad.php
Normal file
153
class_livesklad.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
class livesklad
|
||||
{
|
||||
protected $token_info = array();
|
||||
protected $api_url = "https://api.livesklad.com";
|
||||
private $login = "";
|
||||
private $password = "";
|
||||
private $cURL = null;
|
||||
private $headers = 'Content-Type: application/x-www-form-urlencoded';
|
||||
|
||||
public $id = "";
|
||||
public $remainRequest = 0;
|
||||
|
||||
public function __construct($login, $password)
|
||||
{
|
||||
if(!isset($login) || !isset($password)) exit();
|
||||
|
||||
$this->login = $login;
|
||||
$this->password = $password;
|
||||
$this->cURL = curl_init();
|
||||
|
||||
curl_setopt_array($this->cURL, array(
|
||||
CURLOPT_RETURNTRANSFER => 1,
|
||||
CURLOPT_REFERER => "http://optima-crimea.ru/",
|
||||
));
|
||||
|
||||
if(file_exists("class_" . __CLASS__ . ".json")) $this->token_info = json_decode(file_get_contents("class_" . __CLASS__ . ".json"), true);
|
||||
$this->__checkToken();
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if($this->cURL)
|
||||
curl_close($this->cURL);
|
||||
}
|
||||
|
||||
//Проверка токена на актуальность
|
||||
private function __checkToken()
|
||||
{
|
||||
if(!isset($this->token_info['token']) || time() - $this->token_info["time"] >= 10 * 60) /* token timeout is 10 min */
|
||||
$this->__getToken();
|
||||
}
|
||||
|
||||
|
||||
//Получение токена, сохранение в файл
|
||||
private function __getToken()
|
||||
{
|
||||
$method = "auth";
|
||||
|
||||
$params = array('login' => $this->login, 'password' => $this->password);
|
||||
|
||||
$result = $this->query($method, $params, false);
|
||||
|
||||
if(isset($result->error))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->token_info = array("token" => $result->token, "time" => time(), "renew" => false, 'headers' => [$this->headers, 'Authorization: ' . $result->token]);
|
||||
file_put_contents("class_" . __CLASS__ . ".json", json_encode($this->token_info));
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected function query($method, $params = [], $get)
|
||||
{
|
||||
// $get = true Метод GET, false Метод POST
|
||||
$url = $this->api_url . "/" . $method;
|
||||
$response = "";
|
||||
|
||||
if(!empty($params) && $get == true)
|
||||
{
|
||||
$url .= "?" . http_build_query($params);
|
||||
}
|
||||
curl_setopt($this->cURL, CURLOPT_URL, $url);
|
||||
|
||||
if($get == true)
|
||||
{
|
||||
curl_setopt($this->cURL, CURLOPT_POST, 0);
|
||||
curl_setopt($this->cURL, CURLOPT_HTTPHEADER, $this->token_info['headers']);
|
||||
$response = json_decode(curl_exec($this->cURL), true);
|
||||
}
|
||||
else
|
||||
{
|
||||
curl_setopt($this->cURL, CURLOPT_POST, 1);
|
||||
curl_setopt($this->cURL, CURLOPT_POSTFIELDS, http_build_query($params));
|
||||
$response = json_decode(curl_exec($this->cURL));
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
public function getOrders ($params = [])
|
||||
{
|
||||
$this->__checkToken();
|
||||
|
||||
$method = "company/orders";
|
||||
|
||||
if(is_array($params) == false)
|
||||
{
|
||||
$params = [];
|
||||
}
|
||||
|
||||
return $this->query($method, $params, true);
|
||||
}
|
||||
|
||||
|
||||
public function getCounteragents ($params = [])
|
||||
{
|
||||
$this->__checkToken();
|
||||
|
||||
$method = "counteragents";
|
||||
|
||||
if(is_array($params) == false)
|
||||
{
|
||||
$params = [];
|
||||
}
|
||||
|
||||
return $this->query($method, $params, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function getBranches($params = [])
|
||||
{
|
||||
$this->__checkToken();
|
||||
|
||||
$method = "shops";
|
||||
|
||||
return $this->query($method, $params, true);
|
||||
}
|
||||
|
||||
|
||||
public function getStatusID()
|
||||
{
|
||||
$this->__checkToken();
|
||||
|
||||
$method = "statuses";
|
||||
|
||||
$params = [];
|
||||
|
||||
$result = $this->query($method, $params, true);
|
||||
|
||||
return $result['data'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user