简体   繁体   English

PHP file_get_contents()遵循Content-length标头

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

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;

as it doesn't pay attention to the Content-length header. 因为它没有注意Content-length标题。 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;

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

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