Api call in another third party api call using guzzlehttp in laravel 8

Updated: Feb 18, 2025

Api call in another third party api call using guzzlehttp in laravel 8

To make an API call in another third-party API call using GuzzleHttp in Laravel 8, follow these steps:

  1. Install GuzzleHttp package: First, you need to install the GuzzleHttp package if you haven't already. You can install it via Composer by running the following command in your terminal:
composer require guzzlehttp/guzzle
  1. Create a helper function: Create a helper function in your Laravel project to make the API call using GuzzleHttp. Here's an example of how to create a helper function in app/Helpers/ApiHelper.php:
<?php

namespace App\Helpers;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class ApiHelper
{
    protected $client;

    public function __construct()
    {
        $this->client = new Client([
            'base_uri' => 'https://api.example.com/', // Replace with the base URL of the third-party API
            'headers' => [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ],
        ]);
    }

    public function callApi($method, $endpoint, $data = [])
    {
        try {
            $response = $this->client->request($method, $endpoint, [
                'json' => $data,
            ]);

            if ($response->getStatusCode() == 200) {
                return json_decode($response->getBody(), true);
            } else {
                throw new \Exception('API call failed with status code: ' . $response->getStatusCode());
            }
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
                $response = json_decode($e->getResponse()->getBody(), true);
                throw new \Exception('API call failed with status code: ' . $e->getResponse()->getStatusCode() . ' and message: ' . $response['message'] ?? '');
            } else {
                throw new \Exception('API call failed: ' . $e->getMessage());
            }
        }
    }
}

Replace https://api.example.com/ with the base URL of the third-party API.

  1. Use the helper function: Now you can use the helper function to make API calls in your Laravel controllers or other helper functions. Here's an example of how to use the helper function in a controller:
<?php

namespace App\Http\Controllers;

use App\Helpers\ApiHelper;

class ExampleController extends Controller
{
    protected $apiHelper;

    public function __construct(ApiHelper $apiHelper)
    {
        $this->apiHelper = $apiHelper;
    }

    public function index()
    {
        try {
            $response = $this->apiHelper->callApi('GET', 'endpoint');
            return response()->json($response);
        } catch (\Exception $e) {
            return response()->json([
                'error' => $e->getMessage(),
            ], 500);
        }
    }

    public function store()
    {
        $data = [
            'name' => 'John Doe',
            'email' => 'john.doe@example.com',
        ];

        try {
            $response = $this->apiHelper->callApi('POST', 'endpoint', $data);
            return response()->json($response);
        } catch (\Exception $e) {
            return response()->json([
                'error' => $e->getMessage(),
            ], 500);
        }
    }
}

Replace endpoint with the endpoint URL of the third-party API.

That's it! You have now created a helper function to make API calls using GuzzleHttp in Laravel 8.