PHP file_get_contents()遵循Content-length标头

时间:2012-06-07 23:14:56

标签: php http-headers file-get-contents content-length http-content-length

我正在使用这样的代码:

//section with the important stuff for the client
ob_start();
echo "Blah... Random Content" . rand(1,1000);
$size = ob_get_length();
header("Content-Length: $size");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
//all the following output/script running time should be ignored by the client (file_get_contents())
sleep(10);
echo "long action completed";

输出一些内容,然后运行耗时的后台作业 在另一个文件中,我试图访问第一个脚本的数据,而不必等待后台作业完成。
不幸的是,这对我不起作用:

$content = file_get_contents("http://some-address/thescript.php");
echo $content;

因为它没有注意Content-length标题。在浏览器中,整个过程工作正常。有什么建议?感谢。

1 个答案:

答案 0 :(得分:2)

修正了它。如果有人遇到同样的问题:


$url = "http://some-adress/test.php";
$headers = get_headers($url, 1);
$content_length = $headers["Content-Length"];
$content = file_get_contents($url, NULL, NULL, NULL, $content_length);
echo $content;


相关问题