Skip to content

Instantly share code, notes, and snippets.

@ThatLadyDev
Last active July 10, 2021 03:01
Show Gist options
  • Save ThatLadyDev/d4a4082a1959b6c766759028758a526e to your computer and use it in GitHub Desktop.
Save ThatLadyDev/d4a4082a1959b6c766759028758a526e to your computer and use it in GitHub Desktop.
How to make a GET request using guzzle with basic authentication (Laravel)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
class GuzzleController extends Controller
{
public function getRequest()
{
$client = new Client();
$url = 'https://api-url.com';
// GET with basic auth
$headers = [
'Content-type' => 'application/json; charset=utf-8',
'Accept' => 'application/json',
'Authorization' => 'Basic {Already encoded auth credentials}',
];
$response = $client->request('GET', $url, [
'headers' => $headers
]);
$info = json_decode($response->getBody()->getContents(), true);
return $info;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment