简体   繁体   中英

How to store and log for all http request and response in wordpress?

I want to store all outgoing http request from my wordpress site. I want to store all internal and external http request and response also request made with curl. Any help should be useful to me.

Hook into WP_Http::_dispatch_request()

function fn_log_http_request_response( $wp_http_response, $request, $url ) {
    $request = [
        'method' => $request['method'],
        'url' => $url,
        'headers' => $request['headers'],
        'body' => $request['body'],
    ];

    if($wp_http_response instanceof WP_Error) {
        $response = [
            'errors' => $wp_http_response->errors,
            'error_data' => $wp_http_response->error_data,
        ];
    } else {
        $response = [
            'status' => [
                'code' => wp_remote_retrieve_response_code($wp_http_response),
                'message' => wp_remote_retrieve_response_message($wp_http_response),
            ],
            'headers' => wp_remote_retrieve_headers($wp_http_response)->getAll(),
            'body' => wp_remote_retrieve_body($wp_http_response),
        ];
    }

    error_log(print_r([
        'request' => $request,
        'response' => $response,
    ], true));

    return $wp_http_response;
}

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

Adopted from hinnerk-a

You can also try the following plugin as well

https://wordpress.org/plugins/log-http-requests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM