I have a function sendSMSMessage() and it runs after printing some results:
<?php
...
print $x;
sendSMSMessage(params);
...
?>
This process of this function might take too long time of running and the app has to wait a long time before getting a response so i would like to print the results regardless of whether the sendSMSMessage has done...
Related
Here is the pseudo code of what I am doing:
<?php
for ($x=0;$x<5;$x++)
{
$result=shell_exec("python code.py");
echo $result;
sleep(15);
}
?>
Here is the code.py pseudo code
try:
condition
print "successful"
except:
print "not successful"
What I am trying to do is, each time the python code is run print its output on the web page instantly, then initiate the sleep timer and repeat.
Instead what I am getting now is the web page stalls for a cumulative 15*5 secs, and displays output of the python code that was run 5 times in one go.
Any suggestions on how this can be done
i want introduce some sleep functionality into my code so that i can call a function at a specified(time or interval).
my desired output:
(1) at first 5sec
im called
(2) at 10sec (i,e 5sec + 5 sec)
im called // at first iteration of loop
im called // at second iteration of loop
right now i'm using
function curl_grab(){
echo "im called<br/>";
}
$arr = ['http://ab.com/','http://bc.com/'];
foreach($arr as $el){
curl_grab($el);
sleep(5);
}
but the problem with the above code is it is dumping everything at a time
like so
im called
im called
i want it one by one
here is demo: http://phpio.net/s/ggr
If you are using a browser to test this code, you will be able to see the output together only, becoz you will see output after execution of php code is completed only. You can execute this in terminal to see the out put when it happens.
Write this code into test.php
function curl_grab(){
echo "im called\n";
}
$arr = ['http://ab.com/','http://bc.com/'];
foreach($arr as $el){
curl_grab($el);
sleep(5);
}
To execute in terminal you can type
$ php /path/to/test.php
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?
With rate limited resources you usually want to cache a copy of the data for blocks of X minutes so that the limit is never actually exceeded. For example, in the case of a maximum of 10 requests per hour you would cache the response for at least 6 minutes before attempting fetch a new response.
It is not a good idea to stall the entire PHP interpreter until the rate limit is lifted.
As for approaching "repeat an attempt to do something until it works" in general, this is not something that PHP handles very well given you usually want a request and response cycle with PHP to be as fast as possible so it can move onto the next request. Your PHP application should provide an immediate yes/no response to an external utility that triggers the task at a given interval.
I solved it like this:
// This will be the testing variable, where in the actual script
// we'll check the response code of file_get_contents
$count = 0;
function funcTwo( &$count ) {
// Here I'd run file_get_contents and parse the headers
$count = ++$count;
echo "functTwo() running $count... \n";
// Here I'll test for response code 429, if true, restart
if ($count !== 5) {
echo "Count only = $count so we're gonna take a nap... \n";
sleep(1);
echo "Waking from sleep $count and rerunning myself... \n";
funcTwo($count);
return;
}
echo "Count finally = $count, exiting funcTwo... \n";
}
// This function does the main work that relies on the successful response from
function funcOne( $count ) {
echo "functOne() running! \n";
// The function will be delayed here until a successful response is returned
funcTwo($count);
echo "Count finally = $count, so now we can finally do the work that \n";
echo "depends on a successful response from funcTwo() \n";
// Do main work
echo "Work done, exiting funcOne and script... \n";
}
funcOne($count);
Is it possible to set a delay between 2 portion of code in PHP? I want something like this-
<?php
function firstFunc(){
echo "anything";
}
function secondFunc(){
echo "something";
}
// call first fumction
firstFunc();
// I want the script to wait for 2 seconds here, then call second function
secondFunc();
Is it possible?
If you want to delay the execution of something, you can use the sleep($seconds) function. Here's what the documentation says
Delays the program execution for the given number of seconds.
It returns zero on success and false on error.
So you can do something like:
<?php
function firstFunc(){
echo "anything";
}
function secondFunc(){
echo "something";
}
// call first function
firstFunc();
sleep(2); //Delays execution for 2 seconds.
secondFunc();
?>
Keep in mind though, PHP is a server side language. The only thing the client will receive from a PHP script(if the client has to receive anything at all) is HTML(or CSS, JS and so on). So when you run this code, the page will show nothing for 2 seconds and then output anything and something together because the delay is happening at a server level, and not on client machine. If you want a kind a delay where something is visible on the screen and then after 2 seconds anything appears, you want to use JavaScript.
Hope this helps :)
You should use function sleep(int seconds)
First, correct me if i'm wrong but i'm under the assumption that when you run the Sleep() function, it pauses running the script where it is located in the script, not at the beginning. If that is true can someone tell me why the below script waits 5 seconds and then shows both echos at the same time. NOT echo the first statement on page load and then wait 5 seconds and then fire the second echo....
echo "Your account username has been updated, you will now be redirected to the home page!";
sleep(5);
echo "REDIRECT!";
In your code PHP execution will pause for 5 seconds but it will not render itself part by part. i.e. It will not show the first statement and then the second. PHP keeps all its value in output buffer and display them when its finishes execution.
What happens is, it holds the value of first echo in output buffer and then waits for 5 seconds, then is holds another echo output in output buffer and shows all at once.
What you are trying to do is a lot easier in JS.
echo "Your account username has been updated, you will now be redirected to the home page!";
echo "<script> document.setTimeout(function() { document.location('redirect.html'); }, 5000); </script>";