PHP implements restful-style APIs

Source: Internet
Author: User
RESTful is a design style rather than a standard, such as an interface originally:

Http://www.test.com/user/view/id/1

Represents getting user information with ID 1, which can be changed if you use restful style:

Http://www.test.com/user/1

The benefits of doing this can be clearly seen:

1, more concise URL, programmer-friendly

2, do not expose the internal code structure, more secure

So, how do you implement this interface? First, we need to receive the/USER/1 section.

$path = $_server[' path_info '); $arr = explode ('/', $path);p Rint_r ($arr);

Get the following result:

Array (    [0] =     [1] = User    [2] = 1)

Get the parameters, the following is a simple operation:

if ($arr [1] = = ' user ') {$model = new Usermodel (); $id = $arr [2];//reads user information $user_info = $model->find ($id); Echo Json_encode ( $user _info);}

In this way, we have implemented a restful style of API.

below, we look at how to implement the interface to read the user list, the traditional way:

Http://www.test.com/user/list

Using restful styles, you can be more concise:

Http://www.test.com/user

And the difference between reading user information is that there is no ID after user, so we can modify it on the basis of the Reading section:

if ($arr [1] = = ' user ') {$model = new Usermodel (); $id = $arr [2];if ($id) {//read user information $user_info = $model->find ($id); Echo JSON _encode ($user _info);} else{//Read user list $user _list = $model->select (); Echo Json_encode ($user _list);}}

This enables the interface to read the list of users.

Let's look at how to implement an additional user interface in the traditional way:

Http://www.test.com/user/add

RESTful style:

Http://www.test.com/user

and read the user list interface is the same, how to distinguish it? In fact, it is very simple, the read is a GET request, and the increase is a POST request, the user information is present in the post parameters, so you can make the following changes to the code:

if ($arr [1] = = ' user ') {$model = new Usermodel (); $id = $arr [2];if ($id) {//read user information $user_info = $model->find ($id); Echo JSON _encode ($user _info);} Else{if (is_post) {//Add user $res = $model->add ($_post), if ($res) {echo ' Success ';} Else{echo ' fail ';}} else{//Read user list $user _list = $model->select (); Echo Json_encode ($user _list);}}

the same interface allows different logic to be executed according to the HTTP request method.

This is the core idea of implementing the RESTful style API, and so on, we can implement the edit user interface and delete the user interface:

Http://www.test.com/user/1

As long as you use different request methods, you can distinguish them. We can use the put request to perform the edit operation, and delete the deletion request.

The specific code is not written, interested friends can try to achieve the following.

Support for restful styles is also provided in the thinkphp framework, which is similar in principle.

To summarize, the restful style is to implement the interface using a simple URL and HTTP request method.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.