簡體   English   中英

如何在 codeigniter 中創建 Rest API

[英]How to create Rest API in codeigniter

為了創建簡單的 Rest API,我按照以下步驟下載CodeIgniter-restserver並將粘貼的REST_Controller從下載的文件復制到我的項目下的庫中( src是項目名稱)。 然后在我的項目的控制器中創建了 Api.php

<?php
require(APPPATH'.libraries/REST_Controller.php');

class API extends REST_Controller {

    function test()
    {
        echo "RESTfull API";
    }
}
?>

And I run the URLpostman And I run the URL http://localhost/src/index.php/Api/test但它沒有顯示結果。

您需要按照以下鏈接https://itsolutionstuff.com/post/codeigniter-3-restful-api-tutorialexample.html

然后在你運行代碼后你會得到一個小錯誤 Unable to load the required language file: language/english/rest_controller_lang.php

問題是 codeigniter 找不到 rest_controller 翻譯。 你只需要創建這個文件 /application/languages/english/rest_controller_lang.php

然后將此代碼復制並粘貼到其中:

<?php
/*
 * English language
 */
$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key
$lang['text_rest_invalid_credentials'] = 'Invalid credentials';
$lang['text_rest_ip_denied'] = 'IP denied';
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized';
$lang['text_rest_unauthorized'] = 'Unauthorized';
$lang['text_rest_ajax_only'] = 'Only AJAX requests are allowed';
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller';
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions';
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method';
$lang['text_rest_ip_address_time_limit'] = 'This IP Address has reached the time limit for this method';
$lang['text_rest_unknown_method'] = 'Unknown method';
$lang['text_rest_unsupported'] = 'Unsupported protocol';

希望這可以幫助

請逐行閱讀本文 這是初學者使用 CodeIgniter Rest API 庫的最佳解決方案。

<?php
require(APPPATH.'/libraries/REST_Controller.php');

class API extends REST_Controller {

    function test_get()
    {

    $data = array("message"=>"RESTfull API");
    $this->response($data);
    }
}
?>

調用: http://localhost/src/index.php/Api/test

注意:在 rest API 中,您應該將方法類型定義為GET, POST, PUT, and DELETE

如何在 CodeIgniter 中使用 Rest API 庫

希望能幫到你 :

require APPPATH . '/libraries/REST_Controller.php';
class Api extends REST_Controller {

    function test_get()
    {
      $data = array('response' => 'RESTfull API');

      if(count($data ) > 0)
      {
         $this->response($data ,REST_Controller::HTTP_OK);
      }
      else
      {
         $error = array('message' => 'No record found');
         $this->response($error,REST_Controller::HTTP_OK);
      }   
}  

更多請閱讀: https : //code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814

嘗試

<?php
    require(APPPATH'.libraries/REST_Controller.php');

    class ApiController extends REST_Controller {

        function test()
        {
            echo "RESTfull API";
            die;
        }
    }
?>

我建議使用庫中的內置方法response

嘗試小寫: api/test而不是Api/test

更新。 添加到 config/routes.php

$route['api/test'] = 'api/test';

另一件事,如果你有路線嘗試弄清楚,他們是否使用復數,如果它是敵人,所以你可以嘗試檢查/apis/test而不是/api/test因為將控制器名稱從一種形式轉換為多種形式(對不起原始英語)

任何Web應用程序API對於與另一個應用程序進行通信都是非常有用的。 Codeigniter中有許多Web教程和現成的庫可在Web上使用API​​。

但是,如果您想學習如何在Codeigniter應用程序中創建自己的API,那么我發現了https://www.webslesson.info/2019/08/codeigniter-3-make-crud-rest-api.html

在本文中,您可以找到如何從頭開始制作API的方法,並且通過使用本文的源代碼,可以根據需要創建自己的自定義方法。 因此,如果您正在尋找Codeigniter REST Ful CRUD API。 然后發布會為您提供幫助。 祝你好運。

暫無
暫無

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

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