php请求网址,无需等待响应

时间:2010-11-16 04:43:12

标签: php

我正在尝试在不等待内容的情况下执行file_get_content BUT的变体。基本上我在不同的URL请求另一个php脚本,它将下载一个大文件,所以我不想等待文件完成加载。任何人都有任何想法?

谢谢!

3 个答案:

答案 0 :(得分:2)

我建议您查看popen函数或curl multi函数。

最简单的方法是:

$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);

如果您不想等待它完成:

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

exec("wget http//www.example.com/myscript.php");

答案 1 :(得分:2)

在下载该文件的脚本上尝试此操作:

//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 !!!

被盗:http://andrewensley.com/2009/06/php-redirect-and-continue-without-abort/

答案 2 :(得分:1)

我成功使用了此帖子中的curl_post_async

How do I make an asynchronous GET request in PHP?