Skip to content

Instantly share code, notes, and snippets.

@manidip
Forked from maheshwaghmare/debug-http-request.php
Created March 24, 2022 11:32
Show Gist options
  • Save manidip/c7b29d3498ca50be7dbf63a78ee6908f to your computer and use it in GitHub Desktop.
Save manidip/c7b29d3498ca50be7dbf63a78ee6908f to your computer and use it in GitHub Desktop.
Debug the HTTP requests in WordPress wp_remote_post() or wp_remote_get() with the help of `http_api_debug` hook.
<?php
/**
* Debug HTTP requests in WordPress
*
* Fires after an HTTP API response is received and before the response is returned.
*
* Output in `wp-content\debug.log` file:
*
* [24-Apr-2019 06:50:16 UTC] ------------------------------
* [24-Apr-2019 06:50:16 UTC] https://downloads.wordpress.org/plugin/elementor.2.5.14.zip
* [24-Apr-2019 06:50:16 UTC] {"errors":{"http_request_failed":["cURL error 28: Resolving timed out after 10518 milliseconds"]},"error_data":[]}
* [24-Apr-2019 06:50:16 UTC] Requests
* [24-Apr-2019 06:50:16 UTC] response
* [24-Apr-2019 06:50:16 UTC] {"method":"GET","timeout":300,"redirection":5,"httpversion":"1.0","user-agent":"WordPress\/5.1.1; http:\/\/astra-sites-dev-test.sharkz.in","reject_unsafe_urls":true,"blocking":true,"headers":[],"cookies":[],"body":null,"compress":false,"decompress":true,"sslverify":true,"sslcertificates":"\/var\/www\/html\/astra-sites-dev-test.sharkz.in\/public_html\/wp-includes\/certificates\/ca-bundle.crt","stream":true,"filename":"\/tmp\/elementor.2.5.14-FOXodB.tmp","limit_response_size":null,"_redirection":5}
* [24-Apr-2019 06:50:18 UTC] ------------------------------
*
* @todo Change the `prefix_` and with your own unique prefix.
*
* @since 1.0.0
*
* @param array|WP_Error $response HTTP response or WP_Error object.
* @param string $context Context under which the hook is fired.
* @param string $class HTTP transport used.
* @param array $r HTTP request arguments.
* @param string $url The request URL.
* @return void
*
* @since 1.0.0
*/
if( ! function_exists( 'debug_wp_remote_post_and_get_request' ) ) :
function debug_wp_remote_post_and_get_request( $response, $context, $class, $r, $url ) {
error_log( '------------------------------' );
error_log( $url );
error_log( json_encode( $response ) );
error_log( $class );
error_log( $context );
error_log( json_encode( $r ) );
}
add_action( 'http_api_debug', 'debug_wp_remote_post_and_get_request', 10, 5 );
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment