Consume api with laravel [closed]

1

I'm looking in the Laravel documentation, in several places, but I'm still unsuccessful. I'm looking for somewhere that has a tutorial on how to consume an api with laravel!

    
asked by anonymous 09.03.2018 / 23:20

1 answer

0

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

See:

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

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

So you need ideas to make requests through your system. For this, we have some options:

  • Pure CURL call with PHP link

  • Call CURL via Shell_exec (for linux); link

  • Some package that abstracts the process: link

  • Regardless of the choice, you will be in any case making a request to another server, in this case providing the API service you need.

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

    An example of this are systems that have Free Market integrations, I have an application like this and, in this case, I set up a service that is responsible for all the negotiations that involve the API. This makes the process easier 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:

  • Make the Service that calls the third API;
  • Create some Route and Controller to call this service;
  • Include in the SQ Crontab so that it is called this link from time to time;
  • Alternative 2:

  • Make the Service that calls the third API;
  • Schedule in Laravel's schedule to run the service from time to time;
  • Alternative 3:

  • In case of third-party API notifications, create a route that receives its data;
  • Configure the API provider to call your route.
  • 09.03.2018 / 23:45