multithreaded programming or alternatively asynchronous HTTP request

Posted on 2006-11-20
Last Modified: 2008-01-09

I'm working on a website, which sends two HTTP requests to other servers. The data sent is logging data, so I don't need to process the HTTP response necessarily.

Is it possible to create a seperate thread in PHP for those HTTP requests?

Or is it possible to send the HTTP requests asynchronously?

As already mentioned, its not necessary to process the HTTP response, but it would be nice if it was possible.

Thanks a lot!
Question by:TheFunkSoulBrother
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Expert Comment

ID: 17978070
Unfortunatley PHP does not directly support threading.

You could send the HTTP requests asynchronously by using the process given at
http:Q_21142767.html "PHP Thread?"

where you make one request - then use fread() to grab the details from the request, whilst you are waiting for the fread to operate you can then make your second requrest
LVL 48

Expert Comment

ID: 17979942
With curl you can process multiple HTTP-requests at the same time. See And curl ist also cappavble of getting the response code from the server.

Author Comment

ID: 17985745
Thanks for the links.

How do I call a script several times (like suggested in http:Q_21142767.html)?
I've got my code in index.php and I want to call e.g. sendHTTPrequest_001.php and sendHTTPrequest_002.php simultaneously - is that possible, or did I get something wrong?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 48

Accepted Solution

hernst42 earned 200 total points
ID: 17985789
If each of the sendHTTPrequest_x.php perform its own request,


$mh = curl_multi_init();

for ($i=1; $i<=2; ++$i) {
  $conn[$i] = curl_init('http://yourservername/sendHTTPrequest_00' .$i . '.php');
  curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
  curl_multi_add_handle ($mh,$conn[$i]);

// start performing the request
do {
  $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active and $mrc == CURLM_OK) {
  // wait for network
  if (curl_multi_select($mh) != -1) {
   // pull in any new data, or at least handle timeouts
   do {
     $mrc = curl_multi_exec($mh, $active);
   } while ($mrc == CURLM_CALL_MULTI_PERFORM);

if ($mrc != CURLM_OK) {
  print "Curl multi read error $mrc\n";

// retrieve data
for ($i=1; $i<=2; ++$i) {
  if (($err = curl_error($conn[$i])) == '') {
  } else {
   print "Curl error on handle $i: $err\n";

LVL 14

Expert Comment

by:Aamir Saeed
ID: 17987049
You could just send the request headers and move on.... fsockopen() would be used here, or cURL

proc_open() could be of interest too, but I wouldn't recommend it.

Assisted Solution

by:John Kawakami
John Kawakami earned 50 total points
ID: 18000894
PHP doesn't do threads, but it does unix process control.

Factor your code into three distinct parts, and use pcntl_fork() to launch clones of yourself, and do the requests.

Personally, I would do something completely different, and a little easier on the server.  I'd write the requests to a queue (a db table) and have a daemon written in perl or c that reads the queue, and requests the URLs, in series.

If I knew how, I'd communicate directly with the daemon via a unix socket, but that's beyond me at this time.  The daemon would take requests and process them asynchronously.  That use the fewest resources.

Author Comment

ID: 18004432
First of all, thanks a lot for your suggestions.
Unfortunately I had no time to implement it an will not have until next week, but it seems to me that curl will work fine.

I think, it would be best to do the logging after a output buffer flush, so the webpage is displayed to the user while the logging runs in the background, right?

I'm developing the application on a Windows server, so i guess pcntl_fork() won't work.

Actually I like most the approach jk2001 suggested, maybe I'll implement it that way.
Did you mean with "communicate via unix socket" that a - for example - C deamon listens for connections (via SOCKET: and PHP sends the logging data to the C deamon (using fsockopen() =>

Author Comment

ID: 18069262
Thank you very much.

Author Comment

ID: 18084004
The approach with CURL multi seems a bit weird to me.
I couldn't find out how to execute code WHILE curl_multi_exec performs the two HTTP requests.
It seems to me that curl_multi_exec performs the HTTP requests at the same time but doesn't allow other code to be executed at the same time. Could that be?

I found another solution, which actually is very simple to implement and works like a charm.
This solution uses simply fsockopen as the requests are performed asynchronously by default.

$fp1 = fsockopen("", 80);
fputs($fp1, "POST HTTP/1.1\r\n");

$fp2 = fsockopen("", 80);
fputs($fp2, "POST HTTP/1.1\r\n");

echo "hello"; // some code which is executed while the two requests are performed
                   // looks like real multithreading!!!

// now get the response of the two HTTP requests
while (!feof($fp1)) $buf1 .= fgets($fp1,128);

while (!feof($fp2)) $buf2 .= fgets($fp2,128);

Author Comment

ID: 18084006
Sorry, the code for creating and sending the HTTP requests should be like this:

$fp1 = fsockopen("", 80);
fputs($fp1, "POST dosomething1.php HTTP/1.1\r\n");

$fp2 = fsockopen("", 80);
fputs($fp2, "POST dosomething2.php HTTP/1.1\r\n");

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question