Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't save the output data of this class using php in databases

<pre lang="PHP"><pre><?php

    class api
    {
        public $apiAddress     = 'https://exaple.com/api-v2/';
        public $connectionMode = 'curl';
        public $outputType     = 'xml';
        public $token          = '';
        public $param          = [];
        public $response       = '';
        public $allMethod      = [
            'cityLoad'           => 'cityLoad',
            'hotelLoad'          => 'hotelLoad',
            
        ];

        public function __construct($token = false, $outputType = false, $connectionMode = false)
        {
            if (!$token) {
                $this->_error('token error!');
            }
            $this->token = $token;
            if ($outputType) {
                $this->outputType = (($outputType == 'xml' or $outputType == 'json' or $outputType == 'html') ? $outputType : $this->outputType);
            }
            if ($connectionMode) {
                $this->connectionMode = (($connectionMode == 'curl' or $connectionMode == 'file') ? $connectionMode : $this->connectionMode);
            }
        }

        public function action($method, $param = [])
        {
            switch ($method) {
                case 'getAllCities':
                    $this->_connection($this->allMethod['cityLoad']);
                    break;
                case 'getAllHotels':
                case 'getHotel':
                    $this->_connection($this->allMethod['hotelLoad'], $param);
                    break;
              
            }
            return $this->response;    }
        private function _connection($method = false, $param = [])
        {
            $action = $this->apiAddress . $this->token . '/' . $this->outputType . '/' . $method;
            if (!empty($param)) {
                $action .= '?' . http_build_query($param);
            }
            if ($this->connectionMode == 'curl') {
                $this->_curlConnection($action);
            } else {
                $this->_fileConnection($action);
            }    }
        private function _curlConnection($action)
        {
            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_URL            => $action
            ));
            $this->response = curl_exec($curl);
            curl_close($curl);
        }
		
        private function _fileConnection($action)
        {
            $this->response = file_get_contents($action);
        }
        private function _error($message)
        {
            echo $message;
            exit;
        }
    }
    $myToken = 'f790000000000024';
    $xmlWithCurl = new api($myToken, 'xml', 'curl');
    $getAllHotels = $xmlWithCurl->action('getAllHotels', ['city' => 'London']);

?>


What I have tried:

down vote
favorite

    I can't save the output data of this class using php in databases gethotelclass.php

I want to store data such as hotel names, latitude and longitude using php in databases.
Posted
Comments
Richard MacCutchan 13-May-18 4:19am    
Where is the code that does the database handling?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900