How to consume APIs inside Laravel?
If you want to fetch, save, crawl, or fire a one-time request to a remote server that exposes API, there are a variety of options for Laravel. An example use-case would be updating the IMDB rating for a batch of movies and series for your film listing web application.
The popular ones are:
You can learn more about how to use it with Laravel HTTP client wrapper in Laravel Doc.
Another powerful option is
curl,you can use it directly with PHP cURL library, or you can use this Laravel package by Ixudra.
Zttp is another wrapper around Guzzle which they describe themselves as `A developer-experience focused HTTP client, optimized for most common use cases.` if you don’t need to send any crazy complex request to your remote, Zttp can be your friend.
Things to keep in mind…
- Always save your remote server credential in `.env` file. Read more about it on Laravel Doc.
- Always check for any error that your remote might throw, it can be as much as YouTube API errors or as little as just access forbidden (403) and not found error (404).
Heads Up! if the API you are going to consume in your application is well-known and complex, there is a high chance that someone else has already developed a library to just work with that API, like this Laravel package by Mustapha Alaouy that specifically calls YouTube API.
How to expose APIs from Laravel?
When you expose one or more endpoints from your application to the world, other users can consume your API to fetch what they are looking for. An example would be to expose all posts of your blog to be consumed by your mobile application team and be shown the posts in the native UI.
A great walkthrough on how to write REST API in Laravel can be found in this blog post in the Twilio blog by Micheal Okoh. Which clearly explains how to do so step by step in Laravel.
If you want to learn more about REST development and be standard, I recommend reading this article on Stack Overflow blog by John Au-Yeung and checking out json:api website for following best practices.
Other than the REST API option, you can consider GraphQL for the fast development of your APIs as it offers query-specific requirements at the frontend. You can also use this GraphQL package for Laravel and match it with Hasura for rapid backend development.
Heads Up! If you want to test your API or someone else’s API, you can use Postman as it has matured dramatically over the past few years to support different requests.