php request url without waiting for response
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 d开发者_JS百科on't want to wait for the file to finish loading. Anyone has any idea?
Thank you!
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");
Try this on the script that will download that file:
//Erase the output buffer
ob_end_clean();
//Tell the browser that the connection's closed
header("Connection: close");
//Ignore the user's abort.
ignore_user_abort(true);
//Extend time limit to 30 minutes
set_time_limit(1800);
//Extend memory limit to 10MB
ini_set("memory_limit","10M");
//Start output buffering again
ob_start();
//Tell the browser we're serious... there's really
//nothing else to receive from this page.
header("Content-Length: 0");
//Send the output buffer and turn output buffering off.
ob_end_flush();
//Yes... flush again.
flush();
//Close the session.
session_write_close();
// Download script goes here !!!
stolen from: http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/
I successfully used curl_post_async
from this thread.
How do I make an asynchronous GET request in PHP?
精彩评论