Of course, you can also build this into your existing MVC systems and take advantage
of the routing there, allowing you to use a URL such as /proxy/api.bit.ly/v3/shorten.
As you can see, with just a little bit of effort, JavaScript (specifically Ajax) and APIs
get along spectacularly well. Whether you use it to access your own APIs or those
of some third party, you can enhance your sites experience with ease.
Developing and Consuming RESTful Services
Perhaps the most important question here is: What is REST and why do I care?
Weve covered some widely used and perfectly adequate service formats already,
and since PHP users have been programming with functions for years, we can
probably do everything we need to with the RPC-style services.
REST stands for REpresentational State Transfer, and is more than an alternative
protocol. Its an elegant and simple way to expose CRUD (Create, Replace, Update,
Delete) functionality for items over HTTP. REST is designed to be lightweight to
take advantage of the features of HTTP as they were originally intendedfeatures
such as the headers and verbs we discussed earlier in this chapter.
REST has gained in popularity over the last few years, yet it is conceptually very
different to the function-based styles that developers are more accustomed to; as a
result, many services described as RESTful are, strictly speaking, not entirely
compatible with that description.
Avoid the Zealots
Whenever you publish a RESTful service, its likely that someone, somewhere
will complain that you have violated one or more principles of RESTand theyre
probably right! REST is quite an academic set of principles which doesnt always
lend itself well to business applications. To avoid criticism, simply market your
service as an HTTP web service instead.
Each of the various types of service that REST offers has its strengths. REST is most
often used in services that are strongly data-related, such as when providing the
service layer in a service-oriented architecture. A RESTful service is often quite a
close reflection of the underlying data storage in an application, which is why its
a good fit in these situations. The concept shift as mentioned can be a negative point
PHP Master: Write Cutting-edge Code114

Get PHP Master now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.