繁体   English   中英

PHP file_get_contents()遵循Content-length标头

[英]PHP file_get_contents() follow Content-length header

我正在使用这样的代码:

//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标题。 在浏览器中,整个过程工作正常。 有什么建议么? 谢谢。

修复。 万一有人有同样的问题:


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM