'is_wp_error and wp_remote_get cURL error 28

I'm using the following code. Assume $url is something like https://example.com/cron/cron.php. I'm getting the failure message, but it's actually working because in the linked script I send out an e-mail and I am receiving the e-mails.

So if it's working, then why is is_wp_error triggering?

  if (is_wp_error($wp_remote_get = wp_remote_get($url))) {
    echo "Failed to get script. Error: " . $wp_remote_get->get_error_message() . " Exiting...";
    exit;
  }

Got error: Failed to get script. Error: cURL error 28: Operation timed out after 5004 milliseconds with 0 out of -1 bytes received



Solution 1:[1]

So it was a bit of a silly issue. wp_remote_get retrieves the http response, but I thought it would just be visiting the url to run the script. The script takes a while to complete, so it must be running into time limits, which is what the error message seems to be suggesting.

I resolved this by just using wp_redirect instead. Would be nice if I can find a way to run the other script without redirecting though.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jordan Carter