PHP flush服务器响应通过";委托书;服务器
我有一个“代理”服务器(a)运行Apache/PHP,请求另一个服务器(B)运行Apache/PHP,使用PHP flush服务器响应通过";委托书;服务器,php,http,Php,Http,我有一个“代理”服务器(a)运行Apache/PHP,请求另一个服务器(B)运行Apache/PHP,使用file\u get\u contents。当用户请求服务器a时,它会请求服务器B。服务器B上的请求需要两分钟才能完成,因此它会很早做出响应,先等待动画,然后是PHPflush(),诸如此类: User ---> Server A (a.php) ---> Server B (b.php) - fil
file\u get\u contents
。当用户请求服务器a时,它会请求服务器B。服务器B上的请求需要两分钟才能完成,因此它会很早做出响应,先等待动画,然后是PHPflush()
,诸如此类:
User ---> Server A (a.php) ---> Server B (b.php)
- file_get_contents to B - flush after 1s
- nothing happens after 1s - response end after 2m
waits 2m <---
<?php
$stream_context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'
)
));
$fp = fopen('http://1.2.3.4/b.php', 'r', false, $context);
while (!feof($fp)) {
echo fread($fp, 8192);
flush();
}
fclose($fp);
?>
User-->Server A(A.php)--->Server B(B.php)
-文件\u获取\u内容到B-1s后刷新
-1s后无任何反应-2m后响应结束
等待2米
b.php的最小示例代码:
<?php
$stream_context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'
)
));
echo file_get_contents('http://1.2.3.4/b.php', false, $stream_context);
?>
<?php
echo 'Loading...';
flush();
// Long operation
sleep(60);
echo 'Result';
?>
Q:
有没有办法让服务器a从服务器B“镜像”早期的
刷新
,从服务器B发送准确的刷新结果 使用fopen/fread代替文件获取内容。
然后你可以在阅读之间刷新
大概是这样的:
User ---> Server A (a.php) ---> Server B (b.php)
- file_get_contents to B - flush after 1s
- nothing happens after 1s - response end after 2m
waits 2m <---
<?php
$stream_context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'
)
));
$fp = fopen('http://1.2.3.4/b.php', 'r', false, $context);
while (!feof($fp)) {
echo fread($fp, 8192);
flush();
}
fclose($fp);
?>
使用fopen/fread代替文件获取内容。 然后你可以在阅读之间刷新 大概是这样的:
User ---> Server A (a.php) ---> Server B (b.php)
- file_get_contents to B - flush after 1s
- nothing happens after 1s - response end after 2m
waits 2m <---
<?php
$stream_context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'
)
));
$fp = fopen('http://1.2.3.4/b.php', 'r', false, $context);
while (!feof($fp)) {
echo fread($fp, 8192);
flush();
}
fclose($fp);
?>
在a.php中我该如何做呢?@migg,这对你有帮助吗?谢谢你的帮助。但我从文档中看到,这可能只是我早期冲洗的一部分,不是吗?假设早期刷新有12kbytes,那么它将刷新前8192个字节,然后等待剩余的字节?使用更少的缓冲区进行读取,例如fread($fp,1)
对于立即刷新每个接收到的字节,我应该如何在a.php中执行该操作?@migg,这对您有帮助吗?谢谢您的帮助。但我从文档中看到,这可能只是我早期冲洗的一部分,不是吗?假设早期刷新有12kbytes,那么它将刷新前8192个字节,然后等待剩余的字节?使用更少的缓冲区进行读取,例如fread($fp,1)立即刷新每个接收字节的代码>