Using WordPress ‘http_api_debug’ PHP action

The http_api_debug WordPress PHP action fires after an HTTP API response is received and before the response is returned.

Usage

add_action('http_api_debug', 'my_custom_http_api_debug', 10, 5);

function my_custom_http_api_debug($response, $context, $class, $parsed_args, $url) {
    // your custom code here
}

Parameters

  • $response (array|WP_Error): HTTP response or WP_Error object.
  • $context (string): Context under which the hook is fired.
  • $class (string): HTTP transport used.
  • $parsed_args (array): HTTP request arguments.
  • $url (string): The request URL.

More information

See WordPress Developer Resources: http_api_debug

Examples

Log all API responses

Log all API responses to a custom log file.

add_action('http_api_debug', 'log_http_api_responses', 10, 5);

function log_http_api_responses($response, $context, $class, $parsed_args, $url) {
    // Create a log file
    $log_file = WP_CONTENT_DIR . '/debug-http-api.log';

    // Format log entry
    $log_entry = sprintf(
        "[%s] %s %s\n",
        date('Y-m-d H:i:s'),
        $url,
        json_encode($response)
    );

    // Append log entry to the log file
    file_put_contents($log_file, $log_entry, FILE_APPEND);
}

Email admin on specific API error

Send an email to the administrator when a specific API returns an error.

add_action('http_api_debug', 'email_admin_on_api_error', 10, 5);

function email_admin_on_api_error($response, $context, $class, $parsed_args, $url) {
    // Check if the response is an error and the URL matches the specific API
    if (is_wp_error($response) && strpos($url, 'https://api.example.com/') !== false) {
        // Send email to admin
        wp_mail(
            get_option('admin_email'),
            'API Error',
            'An error occurred while connecting to the Example API: ' . $response->get_error_message()
        );
    }
}

Modify response headers

Modify the Cache-Control header of a specific API response.

add_action('http_api_debug', 'modify_cache_control_header', 10, 5);

function modify_cache_control_header($response, $context, $class, $parsed_args, $url) {
    // Check if the URL matches the specific API
    if (strpos($url, 'https://api.example.com/') !== false) {
        // Modify Cache-Control header
        $response['headers']['Cache-Control'] = 'private, no-cache, no-store, max-age=0';
    }
}

Count successful API requests

Count the number of successful API requests made during a page load.

add_action('http_api_debug', 'count_successful_api_requests', 10, 5);

function count_successful_api_requests($response, $context, $class, $parsed_args, $url) {
    // Check if the response is not an error
    if (!is_wp_error($response)) {
        // Increment the counter
        $GLOBALS['successful_api_requests_count']++;
    }
}

Add a custom user agent to all API requests

Add a custom user agent to all outgoing API requests.

add_action('http_api_debug', 'add_custom_user_agent', 10, 5);

function add_custom_user_agent($response, $context, $class, $parsed_args, $url) {
    // Set custom user agent
    $parsed_args['headers']['User-Agent'] = 'My Custom User Agent';

    // Update the request arguments
    $response = wp_remote_request($url, $parsed_args);
}