【问题标题】:Retrieve data from codeigniter REST API (web service) with pagination使用分页从 codeigniter REST API(Web 服务)检索数据
【发布时间】:2017-01-02 15:33:22
【问题描述】:

如何通过分页从 Web 服务中检索数据?现在我可以显示数据。但我不能进行分页。 我正在使用带有 curl 库的 codeigniter 3.0.6。这是我用来制作 REST API 的:

function index_get($limit = 100, $offset = 0) {
    $MhswID = $this->get('MhswID');
    if ($MhswID == '') {
        $mhsw = $this->db->get('mhsw', $limit, $offset)->result();
    } else {
        $this->db->where('MhswID', $MhswID);
        $mhsw = $this->db->get('mhsw', $limit, $offset)->result();
    }
    $this->response($mhsw, 200);
}

这是用于检索数据:

function __construct() {
    parent::__construct();
    $this->API="http://admin:1234@localhost/ci3_rest/index.php";
}

// menampilkan data mahasiswa
function index($limit = "", $offset = ""){
    $data['mahasiswa'] = json_decode($this->curl->simple_get($this->API.'/mahasiswa'));
    $this->load->view('welcome_message',$data);
}

【问题讨论】:

  • 请提供更多详细信息并显示您的代码。
  • 编辑了@parth。请检查。

标签: codeigniter rest api curl


【解决方案1】:

尝试将函数名改为index_post

function index_post() {
    $limit = $this->post('limit'); 
    $offset = $this->post('offset');
    $MhswID = $this->post('MhswID');
    if ($MhswID == '') {
        $mhsw = $this->db->get('mhsw', $limit, $offset)->result();
    } else {
        $this->db->where('MhswID', $MhswID);
        $mhsw = $this->db->get('mhsw', $limit, $offset)->result();
    }
    $this->response($mhsw, 200);
}

并将 api url 更改为

function index($limit = 100, $offset = 0){
    $data['mahasiswa'] = json_decode($this->curl->simple_get($this->API.'/mahasiswa/index/', array('limit' => $limit, 'offset' => $offset));
    $this->load->view('welcome_message',$data);
}

您可以通过以下方式查看api的响应

var_dump($this->curl->simple_get($this->API.'/mahasiswa/index'));

我认为您必须在 curl 中传递 REST api 的用户名和密码。我不知道您使用的是哪个库,它必须在 REST api 库的文档中给出。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
  • 2021-09-08
  • 2012-10-28
相关资源
最近更新 更多