laravel call api code example
Example 1: laravel api response trait
trait RespondsWithHttpStatus
{
protected function success($message, $data = [], $status = 200)
{
return response([
'success' => true,
'data' => $data,
'message' => $message,
], $status);
}
protected function failure($message, $status = 422)
{
return response([
'success' => false,
'message' => $message,
], $status);
}
}
Example 2: laravel http client
composer require guzzlehttp/guzzle
Example 3: laravel api routes
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Example 4: laravel http client
use Illuminate\Support\Facades\Http;
$response = Http::get('http://test.com');