How can Sending multi http request at the same time?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hiposang
Forum Newbie
Posts: 3
Joined: Mon Jul 18, 2005 3:59 am
Contact:

How can Sending multi http request at the same time?

Post by hiposang »

Hi all,
Please tell me the way we sending and retrieving multi http request at the same time.
Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
hiposang
Forum Newbie
Posts: 3
Joined: Mon Jul 18, 2005 3:59 am
Contact:

Post 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?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I think it'd be possible to achieve using 3 sockets all set as non-streaming and multiplexing between them. Complicated though.
hiposang
Forum Newbie
Posts: 3
Joined: Mon Jul 18, 2005 3:59 am
Contact:

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post 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 ;) :)
Post Reply