PHP file_get_contents()遵循Content-length标头 [英] PHP file_get_contents() follow Content-length header
本文介绍了PHP file_get_contents()遵循Content-length标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用这样的代码:
i'm using code like this:
//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";
输出一些内容并随后运行耗时的后台工作。
另一个文件我试图访问第一个脚本的数据,而不必等待后台作业完成。
不幸的是,这里对我不起作用:
to output some content and subsequently running a time consuming background job.
In a other file i'm trying to access the data of the first script without having to wait for the background job to finish.
Unfortunately this here doesn't work for me:
$content = file_get_contents("http://some-address/thescript.php");
echo $content;
因为它没有注意Content-length标头。在浏览器中,整个过程工作得很好。有什么建议么?谢谢。
as it doesn't pay attention to the Content-length header. In the browser the whole thing works fine though. Any suggestions? Thanks.
推荐答案
修正了它。万一有人遇到同样的问题:
Fixed it. In case anyone has the same problem:
$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;
这篇关于PHP file_get_contents()遵循Content-length标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文