CodeIgniter REST API and PhoneGap


CodeIgniter REST API and PhoneGap

我是CodeIgniter的新手,最近听说了RESTful API,因为我在PhoneGap(monaca)中构建了一个Android应用程序,因此我不得不研究如何使用PHP后端来支持它。

我了解到,使用CodeIgniter restful API和AJAX/JSON,我可以调用并显示数据库中的频道名称、徽标、频道URL和描述等数据,以显示在我的应用程序中。但我看的许多教程要么是西班牙语的(我不会说话,所以我只是坐着看视频),要么就是解释不够。

因此,我发现很难相信我可以通过带有get动词的URL字符串同时调用大约5个频道的详细信息。很难解释,这就是我有多困惑。

如果有一本书或视频可以让我深入了解这方面的知识,我会很高兴。我更喜欢视觉化,所以看一个循序渐进的教程对我有好处。如果没有,一个简短的好解释可以帮助我实现目标。

为了简单起见,我将使用PDO:

URL-给出的示例有2个"通道"

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));
        }
    }
}