Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everdaniel/cecf2e8aeedc9c4d9571c9b0e6f7aa35 to your computer and use it in GitHub Desktop.
Save everdaniel/cecf2e8aeedc9c4d9571c9b0e6f7aa35 to your computer and use it in GitHub Desktop.
Laravel Logging Http Client Request and Response
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('http_logs', function (Blueprint $table) {
$table->id();
$table->string('url');
$table->string('method');
$table->smallInteger('status_code');
$table->text('request_body');
$table->text('response_body');
$table->text('request_headers');
$table->text('response_headers');
$table->decimal('response_time',5,2);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('http_logs');
}
};
<?php
namespace App\Providers;
use App\Models\HttpLog;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Support\ServiceProvider;
use Event;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Event::listen(ResponseReceived::class, function (ResponseReceived $event) {
HttpLog::create([
'url' => $event->request->url(),
'method' => $event->request->method(),
'status_code' => $event->response->status(),
'request_body' => $event->request->body(),
'response_body' => $event->response->body(),
'request_headers' => json_encode($event->request->headers()),
'response_headers' => json_encode($event->response->headers()),
'response_time'=> $event->response->transferStats->getHandlerStats()['total_time']
]);
});
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class HttpLog extends Model
{
use HasFactory;
protected $fillable = [
'url',
'method',
'status_code',
'request_body',
'response_body',
'request_headers',
'response_headers',
'response_time'
];
}
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
// Here we are triggering the actual API call for testing
Http::get('https://jsonplaceholder.typicode.com/todos');
return view('welcome');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment