Laravel

laravel http request to external api

Pinterest LinkedIn Tumblr

here’s an example of making a GET request to an external API using Laravel’s built-in HTTP client, starting with the installation of the http client.

  1. First, you’ll need to install the http client by running the following command in your terminal:
Copy codecomposer require guzzlehttp/guzzle
  1. Next, in the controller you want to make the request from, you can use the following code snippet to make a GET request:
Copy codeuse GuzzleHttp\Client;

class ExampleController extends Controller
{
    public function makeApiCall()
    {
        $client = new Client();
        $response = $client->get('https://api.example.com/endpoint');
        $data = json_decode($response->getBody());
        // do something with the data
        return $data;
    }
}

This will make a GET request to the specified endpoint and then decode the response body into a PHP object. Then you can do something with the data

Note: This is a basic example and you might want to handle errors and exceptions

Hope this helps!