0

我正在实现自己的 API。我在这里学习教程。即使我遵循它,我也很难让我的 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);
  }
}

我期望的是当我访问这个 url 时http://localhost/website/index.php/call/news。我将收到 JSON 响应。但我得到的是 {"status":false,"error":"Unknown method"}。我找不到问题所在。

4

1 回答 1

1

从这里下载或克隆分支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 开发环境工具调试 API

于 2017-08-08T14:09:59.450 回答