簡體   English   中英

Codeigniter RESTful API-如何使用post方法獲取數據

[英]Codeigniter RESTful API - How to get data with post method

我正在使用Codeigniter RESTful API將數據發送到我的Backbone應用程序。 例如這樣:

public function a_get()  
{ 
    $this->load->database();
    $sql = "SELECT artist_id, formated_name FROM `artists` WHERE formated_name LIKE 'A%' LIMIT 0, 50";
    $query = $this->db->query($sql);
    $data = $query->result();

    if($data) {
        $this->response($data, 200);
    } else {
        $this->response(array('error' => 'Couldn\'t find any artists with letter a!'), 404);
    }
} 

無論如何,我必須刪除LIMIT -statement,以便顯示所有數據,在這種情況下,Artists的開頭字母為“ A”,這意味着數據量很大。 為此,我不能使用GET方法,因為它會在LIMIT -statement上截斷字符串,因此我必須使用POST方法,但我不知道該如何實現。 有人知道如何解決嗎?

提前致謝

如果您使用philsturgeon的庫 ,則:

$this->get('blah'); // GET param
$this->post('blah'); // POST param
$this->put('blah'); // PUT param

這是在readme.md上。

Dunno(如果您嘗試過此操作),但可以說您的POST發送時帶有$ _POST ['letter']。 還記得過濾數據。 您可能會發現在Models中可以使用它來執行查詢或數據修改之類的操作,但是我認為您最終會使用它的。

public function a_post()  
{ 
    $letter = ucfirst(substr(mysql_real_escape_string($this->post('letter')), 0, 1));
    $this->load->database();
    if(isset($letter) && !empty($letter))
    {
        $sql = "SELECT artist_id, formated_name FROM `artists` WHERE formated_name LIKE '".$letter."%' LIMIT 0, 50";
        $query = $this->db->query($sql);
        $data = $query->result();

        if($data) {
            $this->response($data, 200);
        } else {
            $this->response(array('error' => 'Couldn\'t find any artists with letter '.$letter.'!'), 404);
        }
    }
    else
    {
        $this->response(array('error' => 'Couldn\'t find any artists because of the empty string!'), 404);
    }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM