Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

roeyhaim's avatar

Http log cUrl command

Hello, Is there any way to execute a function before any Http (the class) request? I need to log any request to full cUrl command. for example: Http::withoutVerifying()->timeout(60)->connectTimeout(10)->get('http://example.com'); need to log: curl -XGET -k -L --connect-timeout=10 --max-time=70 --url 'http://example.com'

Thanks

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve logging of HTTP requests as cURL commands in Laravel, you can use a middleware or a macro to intercept and log the requests. Here's a solution using a macro to extend the Http facade:

  1. Create a Service Provider: You can create a service provider to register the macro.

  2. Register the Macro: In the service provider, you can define a macro that logs the request as a cURL command.

  3. Log the Request: Use the macro to log the request before it is sent.

Here's how you can implement this:

  1. Create a Service Provider (if you don't have one already):

    Run the following command to create a service provider:

    php artisan make:provider HttpLoggingServiceProvider
    
  2. Register the Macro in the Service Provider:

    Open the newly created HttpLoggingServiceProvider and add the following code:

  3. Register the Service Provider:

    Add the service provider to the providers array in config/app.php:

    'providers' => [
        // Other Service Providers
    
        App\Providers\HttpLoggingServiceProvider::class,
    ],
    
  4. Use the Macro in Your HTTP Requests:

    Now, you can use the logAsCurl macro in your HTTP requests:

    use Illuminate\Support\Facades\Http;
    
    $response = Http::logAsCurl()
        ->withoutVerifying()
        ->timeout(60)
        ->connectTimeout(10)
        ->get('http://example.com');
    

This setup will log each HTTP request as a cURL command in your application's log files, allowing you to see exactly how the request is being made.

Please or to participate in this conversation.