Page 1 of 1

How can Sending multi http request at the same time?

Posted: Mon Jul 18, 2005 4:19 am
by hiposang
Hi all,
Please tell me the way we sending and retrieving multi http request at the same time.
Thanks

Posted: Mon Jul 18, 2005 6:58 am
by Chris Corbyn
I don't understand the question.

Could you explain in a little more detail please.

Maybe fsockopen is what you're looking for?

Posted: Mon Jul 18, 2005 7:22 am
by bokehman
You can't does simultaneous multiple anything in php as it is a single thread language. You could of course do it concurrently with a loop.You're not going to be using this for a DOS attack I hope.

Posted: Mon Jul 18, 2005 7:29 am
by Chris Corbyn
bokehman wrote:You're not going to be using this for a DOS attack I hope.
Umm... That hadn't crossed my mind.

What do you need this for exactly?

Posted: Mon Jul 18, 2005 10:25 pm
by hiposang
Sorry because of my unclear question.
Here is the code I send one http request

Code: Select all

for ($i=0; $i<3; $i++) {
     &http = new httpClient();
     if ($http->Connect('www.ups.com', 80)) {
		$http->addHeader('Host', 'www.ups.com');
		$http->addHeader('User-Agent', 'osCommerce');
		$http->addHeader('Connection', 'Close');
		
		//echo &quote;attempt : &quote; . $request . &quote;<br>\n&quote;;
		
		if ($http->Get('/using/services/rave/qcostcgi.cgi?' . $request)) $body = $http->getBody();
		$http->Disconnect();
	} else {
		return 'error';
	}
}
The problem is when I send 3 http requests it takes a lot time to wait each $http call $http->Get() functions. Can we make these http requests asynchronously? Can we have another way to sent all http requests and retrieving it later?

Posted: Tue Jul 19, 2005 2:48 am
by onion2k
I think it'd be possible to achieve using 3 sockets all set as non-streaming and multiplexing between them. Complicated though.

Posted: Wed Jul 20, 2005 6:20 am
by hiposang
Hi onion2k,
Can you tell it in detail? Do you have any documents about it?
Another way, in "for" loop statement, can we raise 3 php pages ,each page take on 1 http request and then retrieve data later?
thanks.

Posted: Wed Jul 20, 2005 7:05 am
by onion2k
hiposang wrote:Hi onion2k,
Can you tell it in detail? Do you have any documents about it?
Another way, in "for" loop statement, can we raise 3 php pages ,each page take on 1 http request and then retrieve data later?
thanks.
http://uk2.php.net/fsockopen
http://uk2.php.net/manual/en/function.s ... ocking.php

Open 3 sockets, set them all as non-blocking, then write proper HTTP requests to each socket to start things going. Then keep looping round using a while loop or something checking if there's anything to read from each one (using stream_get_contents() ). Keep looping until you have all the required data or else they timeout.

That should work. But like I said before, writing the code will be complicated.

Posted: Wed Jul 20, 2005 5:03 pm
by IceMetalPunk
You could always use a FOR loop, and include PHP files. So if you wanted 3 PHP pages, you make 1 that receives the files to call, like this:

Code: Select all

<?php
for ($i=0; $i<count($files); $i++) {
include $files[$i];
}
?>
$files would be an array containg all the files to include. It wouldn't be simultaneous, but it will be darn close.

-IMP ;) :)