For the purpose of using guzzle in Laravel, I will directly dig into the implementation part.

I am assuming you have already set up the laravel project. If not, you can follow my previous tutorial at https://dev.to/jsafe00/set-up-laravel-project-in-a-virtual-machine-with-laravel-homestead-3d4a.

Getting Started

composer require guzzlehttp/guzzle

Install Guzzle Client in Laravel Project. Then create a function for your third party API. I will be using the open weather map API. https://openweathermap.org/

public function openWeatherApiResult()
    { 
    	$client = new Client();
    	
        $apiKey = env('OPEN_WEATHER_API_KEY');
    	$response = $client->request('GET', 'https://api.openweathermap.org/data/2.5/weather?q=' . $this->location . "&appid=" . $apiKey);
    	$statusCode = $response->getStatusCode();
    	$body = $response->getBody()->getContents();
    	$data =json_decode($body);

    	return $data;
    
    }
$client = new Client();

Create a new instance of guzzle client and assign it to $client.

$apiKey = env('OPEN_WEATHER_API_KEY');

Get the Open Weather API key in the env file.

$response = $client->request('GET', 'https://api.openweathermap.org/data/2.5/weather?q=' . $this->location . "&appid=" . $apiKey);

Call request() method. Then assign first param 'GET' for making get request and the second parameter is the third party API URL with the API key.

$statusCode = $response->getStatusCode();

Get the HTTP status code.

$body = $response->getBody()->getContents();

Return the contents

$data =json_decode($body);

Convert result to json

Below is the full code in my controller. Import Guzzle Client and call the third party API function.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use GuzzleHttp\Client;

class WeatherApiController extends Controller
{
	private $location;

    public function index(Request $request)
    {
    	 $cityData = ucfirst($request['city']);
    	 $countryCodeData = strtoupper($request['countryCode']);
    	 
    	 $this->location =  $cityData.','.$countryCodeData;
    
    	 try{
    	 $result['status'] = 200;
    	 $result['data'] = $this->openWeatherApiResult();

    	 } catch (Exception $e) {
    	 		$result = [
    	 			'status' => 500,
    	 			'error' => $e->getMessage()
    	 		];
    	 }
    	  return response()->json($result, $result['status']);
	}

    public function openWeatherApiResult()
    { 
    	$client = new Client();
    	
        $apiKey = env('OPEN_WEATHER_API_KEY');
    	$response = $client->request('GET', 'https://api.openweathermap.org/data/2.5/weather?q=' . $this->location . "&appid=" . $apiKey);
    	$statusCode = $response->getStatusCode();
    	$body = $response->getBody()->getContents();
    	$data =json_decode($body);

    	return $data;
    
    }
}  

Sample Output:

Alt Text