Learn the RESTful architecture

Source: Internet
Author: User
Tags soap representational state transfer what interface couchdb

First, the introduction of the restful

1. A software architecture style, design style rather than standard, just provides a set of design principles and constraints. It is mainly used for client and server interaction classes of software. The design based on this style of software can be more concise, more layered, more easy to implement caching mechanisms.

Rest (English: Representational State Transfer, abbreviated as REST) describes a schema-style network system, such as a Web application. It first appeared in 2000 with Roy Fielding's doctoral dissertation, one of the main authors of the HTTP specification. In the current mainstream of the three Web service interaction scenarios, rest is simpler than soap (Simple Object Access Protocol, the basic objects accessing protocol) and XML-RPC, both for URL processing and payload encoding. Rest tends to be designed and implemented in a simpler, lighter-weight way. It is important to note that rest does not have a clear standard and is more like a design style.

Principle conditions
REST refers to a set of schema constraints and principles. Applications or designs that meet these constraints and principles are RESTful.
The most important REST principle of a WEB application is that the interaction between the client and the server is stateless between requests. Each request from the client to the server must contain the information necessary to understand the request. If the server restarts at any point in time between requests, the client will not be notified. In addition, stateless requests can be answered by any available server, which is ideal for environments such as cloud computing. Clients can cache data to improve performance.

On the server side, application state and functionality can be categorized into various resources. A resource is an interesting conceptual entity that is exposed to the client. Examples of resources are: application objects, database records, algorithms, and so on. Each resource uses a URI (Universal Resource Identifier) to get a unique address. All resources share a unified interface to transfer state between the client and the server. Standard HTTP methods are used, such as GET, PUT, POST, and DELETE. Hypermedia is the engine of application state, and resources are interconnected through hyperlinks.

Http://baike.baidu.com/view/5798116.htm


2.REST (representational state transfer) is a design and development method for network applications, which can reduce the complexity of development and improve the scalability of the system. Rest presents some design concepts and guidelines:
1, all things on the network are abstracted as resources (resource);
2, each resource corresponds to a unique resource identifier (resource identifier);
3, through the Universal Connector interface (Generic connector interface) to operate the resources;
4, the resources of the various operations will not change the resource identification;
5, all operations are stateless (stateless).

It is important to note that rest is a design style rather than a standard. Rest is often based on the use of Http,uri, and XML as well as the HTML of these existing widely popular protocols and standards.

The traditional request mode differs from the request mode of the rest mode:

function Traditional Mode Rest Mode
List all the users Get/users/list Get/users
List user information with ID 1 Get/users/show/id/1 Get/users/1
Insert a new user Post/users/add Post/users
Update user information with ID 1 Post/users/mdy/id/1 Put/users/1
Delete a user with ID 1 Post/users/delete/id/1 Delete/users/1

For more REST information, refer to: Http://zh.wikipedia.org/wiki/REST


Two. Benefits of using RESTful


A. I understand that your question should be "what is the use of changing a traditional PHP web interface into a RESTful web interface?" ”

In fact, it is better to use the word "advantage" instead of "usefulness", because the traditional web interface implementation is also able to achieve business needs (so this is not a necessary thing, need to decide whether to use the overall business needs), and the use of "restful style" will have some additional "benefits":

A unified style makes it easier for the parties to interact and better compatible (this is a common benefit of all that follows a certain norm, just as everyone speaks Mandarin, and it's easier to communicate.)

The operation of the resource exactly corresponds to the corresponding HTTP action (GET, POST, PUT, DELETE), and these actions just can satisfy our need to operate the resource state, that is, what kind of action to choose the state of the resource, And these actions are provided by the HTTP protocol itself, how harmonious and natural ah (is the title of the main extract content)

The impact of the request is clear, or the side effects are clear, such as get must be secure, put and delete must be idempotent, post is definitely unsafe (this is definitely based on the design of the API completely follow the "restful style")

Good "restful style" uri design, can make the function and the overall structure of the Web interface more clear, just through the URI can easily infer what interface is to do, and the correlation between multiple resources

Use HTTP content negotiation to implement multiple representations of a resource, such as a requester's ability to put the format it needs into the Accept field of the header information (such as Accept:text/json), This way the same URI can output multiple formats and no longer need to add a Type=json parameter to the URI.

Client, proxy server, etc. can be processed according to the HTTP protocol specification, such as cache

... There may be some benefits I have not summed up (there will be some deficiencies, of course).

above, the main is to enumerate the "restful style" and the traditional design of the advantages of comparison, but the most important and let the reason for restful, should be restful in the implementation of network Services (Web service) than xml-rpc/ Soap and other protocols more lightweight, with better transparency and scalability caused by this can be explained a lot, no longer repeat, recommend the following 2 partial articles for further understanding.

Nanyi: Understanding the RESTful architecture
REST vs Xml-rpc vs Soap–pros and cons

Why is b.restful popular? Plainly, for one reason: simple.

For API consumer, developing and debugging restful APIs requires only curl.

No client code is generated automatically compared to soap. Compared to thrift, restful also has the huge overhead that the HTTP protocol brings.

C. Things that do not understand can be temporarily not touched, if needed to learn again. If you're in the mood for restful words, Wikipedia has a bunch of links to learn about its meaning, its role, its applications, and related technologies such as soap.

Rest is an HTTP-level thing that has nothing to do with PHP or any other language. It is a custom of the customary HTTP request semantics, or the habit of opening and managing server resources, not something specific to the technical level. As with all the other habits, conventions, you can totally not abide by it, while observing restful nature will bring some advantages (such as get and put are idempotent, when initiating and processing requests can be a lot less to verify the consistency of the logic).

A good example of this is Couchdb's restful api:http://wiki.apache.org/couchdb/http_document_api.

D. The other answers are good, and here's a brief look at what I think from another angle.

If you're just doing a one-time project, rest is an option, you just have to get the data right.

If you are doing your own product, then for the maintainability of the code, you may refer to the rest standard, the team unified, can rest on rest, the purpose is to reduce communication costs, improve productivity, morale (let everyone feel that they are in a NB team)

If you are an open platform, especially a world-class open platform such as FB and Twitter, then you have no choice, the whole world is watching, a change in the API will lead to countless saliva and news reports, when it may also cause bugs. Please weigh the situation yourself.

Personal opinion: I generally work in the second state, the pursuit of production efficiency. Can clearly tell you, rest can not guarantee the productivity of the increase, appropriate use, see the situation to use, to learn but do not tangle.

E. My advice is

1, change a language.
I also find it hard to believe that I have not been exposed to any knowledge of HTTP for the year I was a beginner of PHP. And when I wrote Python and node. js, I ran into it the next day. Forcing themselves to inquire about information, and then learned.
The same problem also, PHP beginners have a few know: buffer,bdd,cgi and so on ...

2, read the "HTTP authoritative guide", a book known as reading a monthly salary at least to get 1w books.

F. Compared to traditional soap, the result of the implementation is the same, that is, the provision of Web service, but in the way and style of access is much simpler

https://segmentfault.com/q/1010000000319475


extended read:
http://www.ruanyifeng.com /blog/2011/09/restful
http://www.ruanyifeng.com/blog/2014/05/restful_ api.html
http://document.thinkphp.cn/manual_3_2.html#restful
https://segmentfault.com/q/1010000000319475
https://segmentfault.com/q/1010000000500665
http://www.baidu.com/s?wd=restful
http://www.sogou.com/web?query=restful
https://www.so.com/s?q=restful
http://www.baidu.com/s?wd=restful%20php
http://www.sogou.com/web?query=restful%20php
https://www.so.com/s?q= restful%20php

Learning RESTful architecture

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.