RestApi使用CodeIgniter发布请求

时间:2017-08-08 11:00:45

标签: codeigniter codeigniter-restserver

我正在实现自己的API。我正在关注the tutorial here。即使我遵循它,我也很难让我的API工作。

我没有得到CodeIgniter REST Server和CodeIgniter REST Client的区别。如果有人向我解释这将是一个很大的帮助。

现在我真正的问题是:我有一个控制器,我扩展了教程中编写的REST_Controller.php。

 class Call extends REST_Controller
 {
   public function news()
   {  
     // initialize you setting 
     $config = array(
        'server' => 'localhost'
     );

     $this->rest->initialize($config);

     // Set method of sending data
     $method = 'post';


     // create your param data
     $param = array(
        'id' => '1',
        'name' => 'test'
     );

     // url where you want to send your param data.
     $uri = 'http://192.90.123.908/api_v1/index.php';

     // set format you sending data
     $this->rest->format('application/json');

     // send your param data to given url using this
     $result = $this->rest->{$method}($uri, $params);

     $data=array(
        'id' => '1',
        'name' => 'test'
     );
     print_r($data);
  }
}

当我访问此网址http://localhost/website/index.php/call/news时,我的期望是什么。我会得到一个JSON响应。但我得到的是 {"status":false,"error":"Unknown method"}。我找不到有什么问题。

1 个答案:

答案 0 :(得分:0)

从此处下载或克隆分支https://github.com/chriskacerguis/codeigniter-restserver

将application / libraries / Format.php和application / libraries / REST_Controller.php文件拖放到应用程序的目录中。要在控制器顶部使用require_once,将其加载到范围中。另外,从应用程序配置目录中的application / config复制rest.php文件。

<?php

require APPPATH . '/libraries/REST_Controller.php';

class Call extends REST_Controller
{
   public function news_get()
   {
     //Web service of type GET method
     $this->response(["Hello World"], REST_Controller::HTTP_OK);
   }
   public function news_post()
   {  
     //Web service of type POST method
     $this->response(["Hello World"], REST_Controller::HTTP_OK);
   }
   public function news_put()
   {  
     //Web service of type PUT method
     $this->response(["Hello World"], REST_Controller::HTTP_OK);
   }
   public function news_delete()
   {  
     //Web service of type DELETE method
     $this->response(["Hello World"], REST_Controller::HTTP_OK);
   }
}

使用Postman Development Environment工具调试API