Last active
July 10, 2021 03:01
-
-
Save ThatLadyDev/d4a4082a1959b6c766759028758a526e to your computer and use it in GitHub Desktop.
How to make a GET request using guzzle with basic authentication (Laravel)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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