代码点火器:Rest api + Json [英] codeigniter: Rest api + Json

查看:29
本文介绍了代码点火器:Rest api + Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现代码点火器rest API,它通过ajax输出JSON.我不确定如何使用 REST API 删除和添加 JSON.如何从 JSON 和数据库中删除项目.

解决方案

1.REST API 路由

让我们从 API 的路由开始 - 如果您将最佳实践应用于 API,您将需要使用 HTTP 动词(POST/PUT/GET...).(在此处阅读有关 HTTP 动词和 REST 的更多信息)

如果您使用的是 Codeigniter 2.x,那么 阅读这个问题和答案

如果您使用的是 Codeigniter 3.x,则 阅读本文档

另一方面,如果您打算仅在内部使用 API 并且项目范围有限,您可以忽略 HTTP 动词,只需对控制器进行 POST 和 GET 调用.

2.创建 JSON 响应

通常您不需要使用视图 - 您可以让您的控制器回显 JSON 响应.下面是一个虚构的 API 示例,该 API 调用 API 从演员数据库中获取演员.

/* ----------------------------------------------|从中获取参与者的 API 方法示例|演员数据库:|调用:http://example.com/api/GetActorById|参数:actor_id|方法:POST*/公共函数 GetActorById(){//我们先从POST数据中获取actor ID$actor_id = $this->input->post('actor_id');//如果 API 方法是 GET,您将获得//URI 上的参与者 ID - API 调用将看起来//像这样:http://example.com/api/GetActorById///在这种情况下,您从片段中获取您的演员 ID:$actor_id = $this->uri->segment(3);//在这里做你的数据库查询魔法!//...//...//让我们用一些数据创建一个数组//在现实生活中,这通常来自上面的数据库查询$数据=数组('名字' =>'迈克尔','姓氏' =>'狐狸','IsActor' =>真的,'用户 ID' =>1234567);//现在让我们将数组转换为 JSON$json = json_encode($data);//如果 API 是从不同的域访问的//您将希望允许跨域访问header('Access-Control-Allow-Origin: *');//现在让我们返回 json回声 $json;}

输出将如下所示:

{"Firstname":"Michael","Lastname":"Fox","IsActor":true,"ActorID":123456789}

希望这会有所帮助 - 祝您的项目好运!

How, do I implement code igniter rest API that outputs with ajax outputting the JSON. I'm unsure how to use the REST API for deleting and adding with the JSON. How to delete a item from the JSON and then from the database.

解决方案

1. Rest API routing

Let's start with the routing of your API - If you apply best practice to your API you will want to use HTTP verbs (POST/PUT/GET...). (Read more on HTTP Verbs and REST here)

If you are on Codeigniter 2.x then read this SO question and answer

If you are on Codeigniter 3.x then read this documentation

On the other side, if you are planning to use the API only internally and the scope of the project is limited you can just ignore HTTP verbs and simply make POST and GET calls to your controllers.

2. Creating JSON response

Generally you will not need to use views - you can have your controllers echo the JSON response. Here is an example of a fictional API that has an API call to get an actor from a database of actors.

/*   ----------------------------------------------
|    Example of an API Method to get an actor from
|    a database of actors:
|    call: http://example.com/api/GetActorById
|    parameter: actor_id
|    Method: POST
*/
public function GetActorById(){

    // Let's first get the actor ID from the POST data
    $actor_id = $this->input->post('actor_id');

    // If the API Method would be GET you would get the
    // actor ID over the URI - The API call would look
    // like this: http://example.com/api/GetActorById/<actor_id>

    // In that case you take your actor ID from the segment:
    $actor_id = $this->uri->segment(3);


    // Do your database query magic here!
    // ...
    // ...

    // Let's create an array with some data 
    // In real life this usually comes from a DB query above
    $data = array(
            'Firstname' => 'Michael',
            'Lastname' => 'Fox',
            'IsActor' => True,
            'UserId' => 1234567
            );

    // Now let's convert the array into a JSON
    $json = json_encode($data);

    // if the API is accessed from a different domain
    // you will want to allow cross domain access
    header('Access-Control-Allow-Origin: *'); 

    // Now let's return the json
    echo $json;
}

The output will look like this:

{"Firstname":"Michael","Lastname":"Fox","IsActor":true,"ActorID":123456789}

Hope this helps - Good luck with your project!

这篇关于代码点火器:Rest api + Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆