PHP file_get_contents() follow Content-length header

我怕爱的太早我们不能终老 提交于 2019-12-12 15:57:23

问题


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. In the browser the whole thing works fine though. Any suggestions? Thanks.


回答1:


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;




来源:https://stackoverflow.com/questions/10941150/php-file-get-contents-follow-content-length-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!