Created
December 29, 2016 09:32
-
-
Save ineersa/485da56d0edc27f2b5fcaa14bb3bd043 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define("METHOD_POST", 1); | |
define("METHOD_PUT", 2); | |
define("METHOD_GET", 3); | |
/** | |
* Class Tezeks_Api | |
* | |
* Wrapper for tezeks api - http://docs.tezeksapi.apiary.io | |
* Usage: | |
* $api = new Tezeks_Api(); | |
* $excursionSearch = new Tezeks_Api_ExcursionSearch(); | |
* $excursionSearch->country = 1; | |
* $data = $api->getExcursions($excursionSearch); | |
*/ | |
class Tezeks_Api | |
{ | |
private $endPointUrl; | |
private $appId = '730094de44935adJ'; | |
private $appSecrete = '82cc2c85dd14103S'; | |
private $uid = null; | |
public $responseCode; | |
public $timeout = 20; | |
public $debug = true; // Note that enabling debug will include debugging information in the response possibly breaking up your code | |
public $rawResponse; | |
/** | |
* Tezeks_Api constructor. | |
* Note - if login password passed they will be used to get uid | |
* @param null|string $login | |
* @param null|string $password | |
*/ | |
public function __construct($login=null,$password=null) | |
{ | |
$this->endPointUrl = "http://tezeks.com/api/v1"; | |
$this->setAppId($login); | |
$this->setAppSecrete($password); | |
$this->getToken(); | |
} | |
/** | |
* @param string $endPointUrl | |
*/ | |
public function setEndPointUrl($endPointUrl) | |
{ | |
$this->endPointUrl = $endPointUrl; | |
} | |
/** | |
* @param string $appId | |
*/ | |
public function setAppId($appId) | |
{ | |
if (!empty($appId)) | |
$this->appId = $appId; | |
} | |
/** | |
* @param string $appSecrete | |
*/ | |
public function setAppSecrete($appSecrete) | |
{ | |
if (!empty($appSecrete)) | |
$this->appSecrete = $appSecrete; | |
} | |
/** | |
* Интерфейс для получения токена авторизации token | |
* /api/v1/auth | |
* @return null|string | |
*/ | |
private function getToken() | |
{ | |
try{ | |
$response = $this->ask('/auth',[ | |
'app_id' => $this->appId, | |
'app_secrete' => $this->appSecrete | |
], METHOD_POST); | |
_d($this->rawResponse); | |
$this->uid = $response['uid']; | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $this->uid; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/1/0?console=1 | |
* @return mixed | |
*/ | |
public function getGenres() | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/references/genres'); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* | |
* @return array|mixed | |
*/ | |
public function getPickups() | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/tourcalendar/stops'); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/3/0?console=1 | |
* @return mixed | |
*/ | |
public function getCountries() | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/references/countries'); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/2/0?console=1 | |
* @param $countryId int | |
* @return mixed | |
*/ | |
public function getCities($countryId) | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/references/cities',[ | |
'countryId' => $countryId | |
]); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/4/0?console=1 | |
* @return mixed | |
*/ | |
public function getExcursionTypes() | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/references/types'); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/6/0?console=1 | |
* @param $excursionSearch Tezeks_Api_ExcursionSearch | |
* @return mixed | |
*/ | |
public function getExcursions($excursionSearch) | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/tours',$excursionSearch->getData()); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/7/0?console=1 | |
* @param $id | |
* @return mixed | |
*/ | |
public function getExcursion($id) | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/tour',[ | |
'id'=>$id | |
]); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/8/0?console=1 | |
* @param $id | |
* @return array|mixed | |
*/ | |
public function getExcursionCalendar($id) | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/tourcalendar',[ | |
'id'=>$id | |
]); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/9/0?console=1 | |
* @param $id | |
* @return array|mixed | |
*/ | |
public function getExcursionGallery($id) | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/tourgallery',[ | |
'id'=>$id | |
]); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/10/0?console=1 | |
* @param $order Tezeks_Api_Order | |
* @return array|mixed | |
*/ | |
public function setOrder($order) | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/order',$order->getData(),METHOD_POST); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/11/0?console=1 | |
* @param $orderSearch Tezeks_Api_OrderSearch | |
* @return array|mixed | |
*/ | |
public function getOrders($orderSearch) | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/order/list',$orderSearch->getData(),METHOD_POST); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* http://docs.tezeksapi.apiary.io/#reference/0/5/0?console=1 | |
* @return mixed | |
*/ | |
public function getExcursionStatuses() | |
{ | |
$response = []; | |
try{ | |
$response = $this->ask('/references/statuses'); | |
} catch(TezeksException $e){ | |
Logs::doLog(json_decode($e->getMessage(),true),'tezeks-api-exception'); | |
} | |
return $response; | |
} | |
/** | |
* This function communicates with API. | |
* You don't need to call this function directly. It's only for inner class working. | |
* | |
* @param string $url | |
* @param array $data | |
* @param int $method See constants defined at the beginning of the class | |
* @return mixed CURL response | |
* @throws TezeksException | |
*/ | |
private function ask($url, $data = [], $method = METHOD_GET) | |
{ | |
//adding uid parameter to all requests except auth | |
if ($url != '/auth'){ | |
if (empty($this->uid)){ | |
$data['uid'] = $this->getUid(); | |
} else $data['uid'] = $this->uid; | |
} | |
$curl = curl_init(); | |
if ($method == METHOD_GET) | |
$url = $this->endPointUrl.$url.'?'.http_build_query($data); | |
else | |
$url = $this->endPointUrl.$url; | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Don't print the result | |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->timeout); | |
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); | |
curl_setopt($curl, CURLOPT_FAILONERROR, true); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($curl, CURLOPT_MAXREDIRS, 3); | |
if ($this->debug) { | |
curl_setopt($curl, CURLOPT_HEADER, true); // Display headers | |
curl_setopt($curl, CURLOPT_VERBOSE, true); // Display communication with server | |
} | |
if ($method == METHOD_POST) { | |
curl_setopt($curl, CURLOPT_POST, true); | |
} else if ($method == METHOD_PUT) { | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); | |
} | |
if (!is_null($data) && ($method == METHOD_POST || $method == METHOD_PUT)) { | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
} | |
try { | |
$return = curl_exec($curl); | |
$this->responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ($this->debug) { | |
_d(curl_getinfo($curl),false); | |
_d($data,false); | |
_d($return,false); | |
} | |
} catch (Exception $ex) { | |
if ($this->debug) { | |
echo "<br>cURL error num: " . curl_errno($curl); | |
echo "<br>cURL error: " . curl_error($curl); | |
} | |
$return = false; | |
} | |
curl_close($curl); | |
$this->rawResponse = $return; | |
$result = json_decode($return,true); | |
if ($result['success']==true){ | |
return $result['data']; | |
} else { | |
$errors = [ | |
'url'=>$url, | |
'data'=>$data, | |
'response'=>$result | |
]; | |
throw new TezeksException(json_encode($errors),400); | |
} | |
} | |
} | |
class TezeksException extends Exception{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment