Asynchronous http GET requests with PHP

I’ve stumbled upon a problem recently – how do you execute slow blocks of code, without locking your script?

The simple answer is – you can’t. At least, not with PHP. In languages like Java or C, where you can use threads (multiple program code blocks, running asynchronously (at the same time)), this is possible to achieve, by simply dispatching another thread, and have your previous one continue to do it’s work. However, PHP doesn’t have threads.

There are two different types of possible situations here:

  1. If you have to wait for the result of the slow block, to continue with your script
  2. If you don’t have to wait for the result

The first problem would be solved in other languages, by either just writing the slow block inline with other parts of code, or if you can do some other work, while the slow block is executing, you can dispatch it in another thread, and then set up an event, once it’s finished to continue with your original script. Once again, you can’t do this with PHP. Well, you could do it with the inline approach, but then, you would have to make sure, that the slow block, does not take more time to execute, than what you have allowed in your php.ini with the max_execution_time directive.

The second problem is possible to solve with PHP. After some googling i have found several people saying they have solved this problem, but none of the solutions acutally wroked. Then, one of them did, using cURL. What you have to do, is you have to put the entire slow block, in  a different PHP script, and then you execute a GET request, using curl_exec, with it’s timeout set to 1. The actual code block how to do it:

$request = "slow_script.php";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_exec($ch);
curl_close($ch);

More information on this can be found at http://nickyaitsky.com/index.php/2007/07/30/asynchronous-php-calls.html. The purpose of that page is to explain how a large file transfer is possible, using this approach.

You can execute several scripts this way too, with curl_multi_exec. Read on more about this here.

A neat thing:

Everyone, who has used google to search for solutions for coding problems that they can’t solve, has stumbled upon http://www.experts-exchange.com/. I personally usually find the solutions provided there to work. The neat thing there, is anyone can post a problem, and then people respond, and the solution that worked, is marked ACCEPTED SOLUTION, and additions to the accepted solution are marked ASSISTED SOLUTION. Problem is you have to pay to actually view those solutions. Or just be curious enough to notice, the long browser vertical scroll bar, and try to scroll all the way down past the question categories… Eureka :)

But another even better way for running a question-answer system is http://stackoverflow.com/. The reason why it is better, is because users can vote on the answers provided, and the highest ranked answers are listed first. Questions are also ranked, by the number of votes that each of question’s responses received. It’s simple and it works. Oh, and yes, this one is free to use :)

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.50 out of 5)
Loading...

5 responses to “Asynchronous http GET requests with PHP”

  1. BestKatrin

    I see you don’t monetize your site, don’t waste your traffic,
    you can earn additional bucks every month.

    You can use the best adsense alternative for any type of website (they approve all websites), for more details simply search in gooogle: boorfe’s tips monetize your website

  2. Giacomo M.

    You rock, man!
    Thank you so much! ;)

  3. ArnoldoChief

    I have checked your blog and i’ve found some duplicate content, that’s why you don’t rank high in google’s search results, but there is a tool that can help
    you to create 100% unique articles, search for: boorfe’s tips unlimited content

  4. Odin

    Excellent solution , work perfect in 2017. Thank you very much you saved my life today :)

  5. abhishek yadav

    A thousands of thanks to you. I was tired googling a way to just trigger another php from one, as i dont have to wait for its result.
    I tried all the method, like exec, pcntl_fork and many more but the api these methods were using were bloked by my host service provide and they were not allowing to make any change in php.in
    So i though no method left which can work, but suddnly i tried above listed one and !!!IT WORKED!!! for me. Thats Really A Greate Info. Thanks a lot.

Leave a Reply