Consume api with Laravel

Asked

Viewed 6,084 times

1

I’m looking in the documentation of Laravel, in several places , but I haven’t had success yet I’m looking for somewhere that has a tutorial how to consume an api with Laravel!

  • Which API you want to consume?

  • my own , already consumed it using Ionic , and very simple has 3 fields only id ,email,senha with Passport ,but not yet tested in the Standard , already used in Postman and Ionic 3

1 answer

0

Regardless of what your Framework is, before consuming something you need to understand what it is to actually consume an API. This will make it easier.

Behold:

An API is nothing more than a set of public (or otherwise) routes (links) that will provide data in a specific format (ultimately JSON).

Note that, in other words, you need to make your Laravel access links and interpret their feedback.

As such, you need ideas for making requests through your system. For this we have a few options:

  1. Pure CURL call with PHP http://php.net/manual/en/curl.examples-basic.php

  2. Call CURL via Shell_exec (for linux); https://incarnate.github.io/curl-to-php/

  3. Some package that abstracts the process: https://medium.com/laravel-5-the-right-way/using-guzzlehttp-with-laravel-1dbea1f633da

Regardless of the choice, you will be anyway making a request to another server, in case what provides the API service you need.

In terms of architecture, I believe that the ideal for this is not to use the controller itself, but rather a layer of Services for this process.

An example of this are systems that have integrations with Free Market, I have an application like this and, in this case, I set up a service that is responsible for all negotiations involving the API. This facilitates the process because once done, I no longer need to think about how the API actually works, just use my mounted service that abstracts the process.

Alternative 1:

  1. Make the Service make the calls in the third API;
  2. Create some Route and Controller to call this service;
  3. Include it in the S.O Crontab to keep calling this link from time to time;

Alternative 2:

  1. Make the Service make the calls in the third API;
  2. Program in Laravel’s Schedule to run the time service in time;

Alternative 3:

  1. In the case of third party API notifications, creates a route that receives data from it;
  2. Configure in API provider to call your route.
  • Yes, I understand how it works already, until already created using Passport , authentication and validation with tokens ,already consumed using in Ionic 3 , now I want to consume it in the Standard

  • See if I can shed some light

Browser other questions tagged

You are not signed in. Login or sign up in order to post.