I have created a simple demo Laravel Nova Tool to fetch some remote data. I need to fetch it with PHP as it requires authorization, anyway this code inside api.php:
$client = new \GuzzleHttp\Client();
Route::get('/employees', function (Request $request) use ($client) {
$base_url = 'https://example.com';
$get_token_url = $base_url . '/auth/access_token?user_id=smthg&user_secret=smthg';
$request = $client->get($get_token_url);
$response = $request->getBody();
return $response;
});
It doesn't work as I'm getting Class 'GuzzleHttp\Client' not found.
I need this dependency inside the Tool. I have installed Guzzle using composer require guzzlehttp/guzzle
inside the Tool and composer.json is updated accordingly. It is a namespace issue. My question is how can I correctly namespace it?
You need to remove the slash before GuzzleHttp\Client.
Also, the $request
parameter gets overridden. In your routes api.php
try this instead.
use Illuminate\Http\Request;
$client = new GuzzleHttp\Client();
Route::get('/employees', function (Request $request) use ($client) {
$base_url = 'https://example.com';
$get_token_url = $base_url.'/auth/access_token?user_id=smthg&user_secret=smthg';
$response = $client->get($get_token_url);
return $response->getBody();
});