Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php request url without waiting for response

Tags:

php

I'm trying to do a variation of file_get_content BUT without waiting for the content. Basically I'm requesting a another php script in different url that will download a large file, so I don't want to wait for the file to finish loading. Anyone has any idea?

Thank you!

like image 526
Patrick Avatar asked Nov 16 '10 04:11

Patrick


People also ask

How do I send a HTTP request without waiting for response?

You cannot just send data without receiving an answer with HTTP. HTTP always goes request -> response. Even if the response is just very short (like a simple 200 with no text), there needs to be a response. And every HTTP socket will wait for that response.

Which protocol doesn't wait for the response from the server?

A connectionless protocol such as UDP doesn't have the three-phase approach like TCP. It just sends the data as soon as it's ready and assumes the endpoint receives it all. UDP expects the application to put the data back together instead of the protocol used in this layer.

Is PHP Curl asynchronous?

Short answer is no it isn't asynchronous.


1 Answers

I would suggest checking out either the popen function or the curl multi functions.

The simplest way would be to do:

$fh = popen("php /path/to/my/script.php");

// Do other stuff

// Wait for script to finish
while (fgets($fh) !== false) {}

// Close the file handle
pclose($fh);

If you don't want to wait for it to finish at all:

exec("php /path/to/my/script.php >> /dev/null &");

or

exec("wget http//www.example.com/myscript.php");
like image 151
Horatio Alderaan Avatar answered Sep 22 '22 02:09

Horatio Alderaan