I'm working on a script that a server posts to us and we post to a server and theoretically is supposed to receive a response from the server and reply back to the server that posted to us. However sometimes the server I'm posting to doesn't respond. The big problem is the server that posted to us will assume the transaction went through successfully unless it hears otherwise. So essentially I need a way to send a response back to the first server if I never hear back from the server I'm posting to. I think register_shutdown_function could work for me but I don't know how to set that up so it only runs on error or whatever.
Thanks in advance
register_shutdown_function()
should work for you. Just use a global variable to determine whether the remote server responded or not:
<?php
function on_shutdown () {
if (!$GLOBALS['complete']) {
// Handle server not responding here
}
}
// Suppress "FATAL ERROR" message when time limit reached
error_reporting(0);
$complete = FALSE;
register_shutdown_function('on_shutdown');
// Code which posts to remote server here
$complete = TRUE;
This means that $complete
will still be false if the code which posts to the remote server does not complete, so you can just check the truthyness of $complete
in the function that fires on shutdown.