0

I use a simple function to send out a 'heartbeat' to a website uptime monitoring service (Better Uptime):

function my_better_uptime_heartbeat() {
    wp_remote_get( 'https://betteruptime.com/api/v1/heartbeat/<heartbeat_ID>' );
}
add_action( 'my_heartbeat', 'my_better_uptime_heartbeat' );

This function is called by wp-cron on a regular schedule but I'm finding that sometimes it isn't sent and I don't know why. I've spent hours with SiteGround, my website hosting provider, and they are telling me everything is fine with the infrastructure.

I would like to:

  1. Check that a HTTP200 code was received from the GET request and
  2. Log any failures (either no status, or anything except HTTP200) to the Wordpress debug log

Does anybody know how to elaborate on the code above to achieve this?

David.


p.s. wp-cron is initiated by the OS's crontab, not on page visits (as would be the norm).

1 Answer 1

3

You can get the response code with wp_remote_retrieve_response_code() and log the response with error_log().

function my_better_uptime_heartbeat() {
    $worked = false;
    $response = wp_remote_get( 'https://betteruptime.com/api/v1/heartbeat/<heartbeat_ID>' );
    if ( ! is_wp_error( $response ) ) {
        if ( 200 == wp_remote_retrieve_response_code( $response ) ) {
            // It worked, no need to log anything.
            $worked = true;
        }
    }
    if ( ! $worked ) {
        // Logs the response that was received for debugging.
        error_log( print_r( $response, true ) );
    }
}
add_action( 'my_heartbeat', 'my_better_uptime_heartbeat' );

This should log failures to WordPress' debug log, assuming that you've got something like the following in your site's wp-config.php:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_DISPLAY', false );
define( 'WP_DEBUG_LOG', true );
4
  • As is of course typical, and somewhat ironic, I've not had any heartbeats fail since implementing your code! But the true branch has been working just fine for days now. I'll keep you posted. It does amaze me how much you people know. I don't think I would have thought of doing that in a million years. Kudos, my friend. Jan 18, 2023 at 18:45
  • 1
    Still no failures so I'll have to conclude that not only did you provide an excellent snippet, but you fixed the underlying problem!! 😂 Many thanks, @Pat J. I am very grateful for your assistance on this. Jan 21, 2023 at 16:57
  • 1
    Finally had a failure and your code was 100% effective! I now have a log of exactly what happens to the wp_remote_get command, thus providing the evidence needed for raising a support ticket. Thank you so much for you help on this, it was driving me insane with each support team blaming the other and me stuck in the middle just wanting the damn thing to work! Jan 28, 2023 at 19:27
  • It seems weird to say "I'm glad it finally broke for you" but I am glad my answer might help you debug an issue.
    – Pat J
    Jan 31, 2023 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.