If some condition is met, how can I make the function:
- Stop executing the rest of the function
- Wait X time
- Restart the function
Would it be something like:
function someFunc() {
if (x == 0) {
sleep(60);
someFunc();
return;
}
...other code only to be run if above is false...
}
someFunc();
...other code only to be run if above function finishes running completely...
In case its relevant and there is some library to handle APi limits or something, I am doing this for an API connection. First I receive a webhook hit via
file_get_contents('php://input')
which contains a URL. Then I hit the URL with
file_get_contents( $url )
and, after parsing $http_response_header
into a $headers
array, check it's header as if ($header['api_limit'] == 0) ...
(in the above example this is x
). If "x"
is 0, then I want the function to wait a minute until the limit cycle resets and run the second file_get_contents( $url )
and the parsing that follows it again.
The main reason I wanted to handle it this way is to not have to record anything. The webhook I receive via file_get_contents('php://input')
only happens once. If API rate limit is hit and I try to use the URL in the webhook but fail, then the URL is lost. So I was hoping the function would just wait X time until the rte resets until trying to use the webhook-received URL with file_get_contents($url)
again. Is this bad practice somehow?