Tarik
Tarik

Reputation: 81711

How to set timeout for specific executions in PHP?

What I am trying to do is, I am running queries against Twitter and if the deliver of the result delays more than a specific time, than I want to say to user that "Please try again later, timeout." I am not asking the Twitter part, I told my story because of the initial explanation but I like to learn how to accomplish it with mostly PHP native codes.

What's in my mind would be something below:

startTimeout(callback(),3000);//Probably this function needs to start a new thread or another concurrent execution scope
//... initiating some twitter queries here

function callback()
{
   echo "Time out, sorry";
}

Upvotes: 0

Views: 312

Answers (2)

Adam Fowler
Adam Fowler

Reputation: 1751

set_time_limit() should do the trick. This defines the number of seconds that the script can run.

Upvotes: 2

genesis
genesis

Reputation: 50966

For your purpose, if you use CURL, try

$timeout = 10;
curl_setopt($resource, CURLOPT_TIMEOUT, $timeout);

for other functionality, it's not so easy to accomplish this.

There are function which already support TIMEOUT parameter, it depends on your purpose

Upvotes: 0

Related Questions