Skip to content

Instantly share code, notes, and snippets.

@hinnerk-a
Created May 31, 2012 20:31
Show Gist options
  • Save hinnerk-a/2846011 to your computer and use it in GitHub Desktop.
Save hinnerk-a/2846011 to your computer and use it in GitHub Desktop.

Revisions

  1. hinnerk-a revised this gist May 31, 2012. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion wp_log_http_requests.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    <?php

    function wp_log_http_requests( $response, $args, $url ) {

    // set your log file location here
    @@ -16,5 +18,8 @@ function wp_log_http_requests( $response, $args, $url ) {

    return $response;
    }

    // hook into WP_Http::_dispatch_request()
    add_filter( 'http_response', 'wp_log_http_requests', 10, 3 );
    add_filter( 'http_response', 'wp_log_http_requests', 10, 3 );

    ?>
  2. hinnerk-a renamed this gist May 31, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. hinnerk-a created this gist May 31, 2012.
    20 changes: 20 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    function wp_log_http_requests( $response, $args, $url ) {

    // set your log file location here
    $logfile = plugin_dir_path( __FILE__ ) . '/http_requests.log';

    // parse request and response body to a hash for human readable log output
    $log_response = $response;
    if ( isset( $args['body'] ) ) {
    parse_str( $args['body'], $args['body_parsed'] );
    }
    if ( isset( $log_response['body'] ) ) {
    parse_str( $log_response['body'], $log_response['body_parsed'] );
    }
    // write into logfile
    file_put_contents( $logfile, sprintf( "### %s, URL: %s\nREQUEST: %sRESPONSE: %s\n", date( 'c' ), $url, print_r( $args, true ), print_r( $log_response, true ) ), FILE_APPEND );

    return $response;
    }
    // hook into WP_Http::_dispatch_request()
    add_filter( 'http_response', 'wp_log_http_requests', 10, 3 );