PHP implements Restful APIs

Source: Internet
Author: User
PHP implements Restful APIs Restful is a design style rather than a standard. for example, an interface is originally like this:

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

Obtains the user information with id 1. if the Restful style is used, it can be changed to the following:

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

We can clearly see the benefits of doing so:

1. more concise URL, friendly to programmers

2. do not expose the internal code structure, making it safer

So how to implement this interface? First, we need to receive/user/1.

$path = $_SERVER['PATH_INFO'];$arr = explode('/',$path);print_r($arr);

The following result is displayed:

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

After obtaining the parameters, the following operations are simple:

If ($ arr [1] = 'user') {$ model = new UserModel (); $ id = $ arr [2]; // read User information $ user_info = $ model-> find ($ id); echo json_encode ($ user_info );}

In this way, we implement a Restful API.

Next, let's look at how to implement the interface for reading the user list. the traditional method is as follows:

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

The Restful style can be more concise:

http://www.test.com/user

The difference with reading user information is that there is no id behind the user, so we can modify it based on the reading part:

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 the user list $ user_list = $ model-> select (); echo json_encode ($ user_list );}}

In this way, the interface for reading the user list is implemented.

The following describes how to add user interfaces. the traditional method is as follows:

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

Restful style:

http://www.test.com/user

It is the same as the interface for reading the user list. how can this problem be distinguished? In fact, it is very simple. reading is a GET request, but adding is a POST request. user information is included in the POST parameter, so you can modify the code as follows:

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 the user list $ user_list = $ model-> select (); echo json_encode ($ user_list );}}}

For the same interface, different logic can be executed based on the HTTP request method.

This is the core idea of implementing Restful APIs, and so on. we can edit and delete user interfaces:

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

As long as different request methods are used, they can be separated. We can use the PUT request to perform the edit operation, and the DELETE request to perform the DELETE operation.

The specific code will not be written. if you are interested, you can try to implement the following.

The ThinkPHP framework also provides support for the Restful style, and the principle is similar.

To sum up, the Restful style is to use simple URLs and HTTP request methods to implement interfaces.

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.