简体   繁体   中英

CodeIgniter REST API and PhoneGap

I am very new to CodeIgniter and I recently heard about RESTful API because I built an Android app in PhoneGap (monaca), consequently I had to research on how to have a PHP backend to support it.

I learnt that with CodeIgniter restful API and AJAX/JSON I can call and display data like channel name, logo, channel URL and description from the database to show inside my app. But many of the tutorials I watched were either in Spanish (I don't speak so I just sit through the video) or it just doesn't explain enough.

Hence I find it hard to believe that I could call about details of about 5 channels at once via a URL string with a get verb. It's hard to explain, that's how confused I am.

If there's a book or video somewhere i can watch to get in-depth knowledge on this, I would be happy. I am more of a visual person so watching a step by step tutorial would be good for me. If not, a brief good explanation could help me archive my goal.

For the sake of simplicity I will be using PDO:

URL - example given has 2 "channels"

http://www.example.com/api/get_channel_info?id[]=2&id[]=67

CodeIgniter

class Api extends CI_Controller
{
    function get_channel_info()
    {
        if(is_array($this->input->get('id')) && $this->input->get('id'))
        {
            $dbh = new PDO($db_conn_string, $user, $pass);

            $sql = 'select * from channels where id in(?'.str_repeat(',?', (count($this->input->get('id')) - 1)).')';

            $sth = $dbh->prepare($sql);
            $sth->execute($this->input->get('id'));

            echo json_encode($sth->fetchAll(PDO::FETCH_ASSOC));
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM