Today, we can connect different applications on different devices which are possible using APIs. API stands for Application Programming Interface and web developers use it to connect different applications irrespective of their platforms to share information. API takes request from different applications, processes the request and gives back the response to the application.
Using APIs one can do the following.
- Build an API to connect with your application from third party applications.
2. Use third party API to connect the third party application and use their information.
3. Build an API to connect your own applications like a mobile app.
What is REST API?
REST stands for Representational State Transfer, and the request and the response should be in an appropriate format. It means that requests should use proper HTTP methods and the response should be in a proper format like JSON or XML.
REST API is a normal API which follows a set of principles. The developer should follow a set of rules while creating and consuming REST API.
REST API Rules
- Use appropriate HTTP methods to send and receive API requests.
Following are the primary HTTP methods.
- GET – To read single or multiple records.
- POST – To create a new record.
- PUT – To update a record.
- DELETE – To delete a record.
- Do not use URL query string for API URLs and use proper URL hierarchy instead.
- Good – http://test.com/api/users/1
- Bad – http://test.com/api/users.php?id=1
- Do not use verbs as resource names in the API URL and use nouns and proper HTTP methods instead.
- Good –http://test.com/api/users
- Bad –http://test.com/api/users/add
- Use plurals for the resource names in the API URL.
- Good –http://test.com/api/users
- Bad –http://test.com/api/user
- Use HTTP response codes to indicate the status of the requests.
- Response data should be in either JSON or XML format.
cURL (HTTP Client Libraries)
Most of you will already know that HTTP stands for Hyper Text Transfer Protocol and it allows us to send information back and forth on the web. To make an HTTP request we need to use one of the HTTP methods (GET, POST, PUT, DELETE, etc).
For using REST APIs, we need a client which has the capability to use all the HTTP methods. Using HTML we can only send GET and POST requests which are not enough to use REST APIs.
That is the reason cURL comes into place. cURL is the most popular and most widely used HTTP client library among PHP developers. If you want to know more about REST API or have any query, feel free to ask in comment section, I will be happy to help you.