If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Asynchronous GET/POST Requests (PHP5)

The following PHP5 snippet defines an AsyncTCP class that will allow you to open and manage multiple requests to remote servers simultaneously.

While I obviously didn't invent the concept I have wrapped what I've seen within a class, added the ability to use either GET or POST, and created a very simple test script to show how it works (to follow).

PHP Code:

<?php

// ##########################################################################// Name: AsyncTCP.php// Date: 09-Sep-2012// Prog: gvroom [at] gmail.com (http://hackedcpu.com/) //// Desc: Provide asynchronous TCP/IP communication. Script will continue to// run and allow retrieval of message (if desired) after some or all of// the base functionality has completed.//// Note: This is not expected to be used (advanced) in a tight loop. Caller// is expected to use sleep() or microsleep() to release CPU.//// Source: http://www.codingforums.com/showthread.php?t=272694// ##########################################################################

// *********************************************************************** // Connect to the remote server. Unfortunately, based on the time taken // to connect this appears not be an asynchronous event... though the read // and write management certainly is. // ***********************************************************************public function connect() {$errno = 0;$errstr = '';$this->s = stream_socket_client($this->transport."://".$this->host.":".$this->port,$errno,$errstr,$this->timeout,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);

This is a run against two Google servers (both GET requests one via SSL). Notice how both connections "advance" to different status values at different times. There may be a bug in the read logic as the first response may be cut off (I'll edit the original post if/when I find it).