-
-
Save ionurboz/7d60cbd89c37c47ff69df2cf0712edfa to your computer and use it in GitHub Desktop.
WordPress filter hook for logging WP HTTP requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
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 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment