@dalton_moen
To make an API call in Laravel, you can follow these steps:
- First, you need to create a controller that will handle the API call. You can create a new controller by running the following command in your terminal:
1
|
php artisan make:controller ApiController
|
- In your ApiController, you can define a method that will make the API call. You can use the Guzzle HTTP client package to make the API call. First, you need to install the Guzzle package by running the following command:
1
|
composer require guzzlehttp/guzzle
|
- Once Guzzle is installed, you can use it in your controller method to make the API call. Here is an example of how you can make a GET request to a URL using Guzzle:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
use GuzzleHttpClient;
public function makeApiCall()
{
$client = new Client();
$response = $client->request('GET', 'https://api.example.com');
$statusCode = $response->getStatusCode();
$body = $response->getBody();
return Response::json(['status' => $statusCode, 'data' => $body]);
}
|
- Finally, you can define a route for your API call in your routes/api.php file:
1
|
Route::get('/api-call', 'ApiController@makeApiCall');
|
Now, when you visit /api-call
on your Laravel application, it will make the API call and return the response.