jQuery + AJAX + PHP: Part 3: Working with APIs

jQuery + AJAX + PHP


Working with APIs

The acronym API stands for Application Programming Interface. When talking to a server through AJAX calls, the communication process is actually working through its API.

Representational State Transfer (REST)

Most web APIs are designed according to a software architecture style called Representational State Transfer (REST). It is very common to see web services advertised as being “RESTful,” meaning they follow this style.

In general, a RESTful API returns either HTML, XML, or JSON formats. (JSON is JavaScript Object Notion.) Depending on how it is accessed or the different options used, many will often supply one or more of these formats as a response to different requests.

Routes and Resources

The REST style uses routes. As part of its design, URLs should be based on the entry requested as broken down into collections and member resources. In turn, these are subdivided by the HTTP verb used to access them.

For example, consider a GET request for “/album/love-is-dead”. The HTTP verb would be GET. The collection would be “album” and the member resource would be “love-is-dead“.

A PUT request for the same resource would look the same, but the internal action would be different. Instead of “getting” data, the use of the PUT verb would signal that data is being “put” into the same resource.

Other HTTP verbs like PATCH (update) and DELETE (remove) can be used the same way.

Server Responses

An API based on the REST style would return different data based on the HTTP verb and resource requested. A PUT request, for example, might get back a response that the operation was successful whereas a GET request might “get” the data it requested from the server.

Carefully reading the documentation for an API can help in understanding how it uses different HTTP verbs and how it sends different responses.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.