login(); } public function __destruct() { $this->logout(); } public function saveAdvert($params = array()) { $result = self::callMethod('addAdvert', array('session_id' => $this->getSessionId(), 'params' => $params)); return $result['advert_id']; } public function delAdvert($sreality_id) { $result = self::callMethod('delAdvert', array( 'session_id' => $this->getSessionId(), 'advert_id' => $sreality_id, 'advert_rkid' => '', )); } public function addPhoto($sreality_id, $data = array()) { $result = self::callMethod('addPhoto', array( 'session_id' => $this->getSessionId(), 'advert_id' => $sreality_id, 'advert_rkid' => '', 'data' => $data)); return $result['photo_id']; } public function delPhoto($sreality_id) { $result = self::callMethod('delPhoto', array( 'session_id' => $this->getSessionId(), 'photo_id' => $sreality_id, 'photo_rkid' => '', )); } protected function login() { self::callMethod('login', array('session_id' => $this->getSessionId())); } protected function logout() { self::callMethod('logout', array('session_id' => $this->getSessionId())); } protected function getSessionId() { if (is_null($this->session_id)) { $result = $this->callMethod('getHash', array('client_id' => self::API_ID)); $this->session_id = $result['sessionId']; } $fp = substr($this->session_id, 0, 48); $vp = md5($this->session_id . self::API_MD5_PASS . self::API_KEY); $this->session_id = $fp . $vp; return $this->session_id; } protected function getRpcClient() { if (is_null($this->client)) { $this->client = new XML_RPC_Client(self::API_PATH, self::API_SERVER); } return $this->client; } protected function callMethod($method, $params, $types = array()) { $client = $this->getRpcClient(); $xml_params = array(); foreach ($params as $key => $param) { if($key == 'data') { $XML_RPC_val = new XML_RPC_Value; $XML_RPC_val->addStruct($param); $xml_params[] = $XML_RPC_val; } else { $xml_params[] = XML_RPC_encode($param); } } $msg = new XML_RPC_Message($method, $xml_params); $response = $client->send($msg); $response = XML_RPC_decode($response->value()); if (!isset($response['status'])) { // var_dump($method); // var_dump($params); // var_dump($response); // die(); throw new Exception('Sreality ERROR: Volání XML-RPC skončilo chybou.'); } if ($response['status'] > 299 || $response['status'] < 199) { // var_dump($method); // var_dump($params); // var_dump($response); // die(); throw new Exception('Sreality ERROR:: ' . $response['status'] . '. ' . $response['statusMessage']); } if (isset($response['output'][0])) { return $response['output'][0]; } } protected function base64EncodeImage($filename) { $imgbinary = fread(fopen($filename, "r"), filesize($filename)); return 'data:image/' . Image::getMimetype() . ';base64,' . base64_encode($imgbinary); } }