Archey
Archey

Reputation: 1342

PHP script to go to url at set interval

So I was curious as to if this was possible. Would it be possible to have a PHP script go to a url at a set interval, say 30 minutes?

Upvotes: 0

Views: 804

Answers (5)

Nick
Nick

Reputation: 783

prggmr allows for intervals to be set using the setInterval function.

Simple example

setInterval(function(){
    echo "30 minutes passed";
, 60000 * 30);

Note this is only available when running using the prggmr command or when running inside the event loop.

More information is available in the documentation.

http://prggmr.org/docs/api.html#api.setinterval

Upvotes: 0

Tadeck
Tadeck

Reputation: 137420

Cron jobs

Yes, just use cron jobs for calling PHP script in 30-minute intervals.

Also see my answer to another question: How to execute PHP code periodically in an automatic way

Delayed Unix call

There is also another option that should work on Unix and involves calling external script with delay without blocking the current script. It may look like this:

exec('( sleep 1800; my_php_script.php) &> /dev/null &');

although the availability of this solution depends on the system and safe_mode settings. For details about exec() function see the documentation.

Upvotes: 4

adiebler
adiebler

Reputation: 135

Only, when you use a cron job. PHP scripts themselves have to be called by a user. With a cron job the php script is called in a given time frame.

http://en.wikipedia.org/wiki/Cron

Upvotes: 0

Jon
Jon

Reputation: 437574

It would certainly be possible (you can use sleep to pause script execution for a period of time, so a while loop that does something and then sleeps will do the trick), but it would not be common to see it happen. In general, scripts in PHP and similar languages are not meant to be kept running indefinitely; you would also have to be careful not to overstep the time limit for script execution (or use set_time_limit to disable it).

Most of the time it's much more appropriate to use a cron job (for Linux) or a scheduled task (for Windows) to arrange for your program to execute every so often.

Upvotes: 3

Grzegorz Łuszczek
Grzegorz Łuszczek

Reputation: 548

Read about Cron. I recommend to run script using PHP interpreter from comand line, rather than wget

Upvotes: 0

Related Questions