douyuan4357 2013-08-07 16:46
浏览 27

stream_get_line在到达时不读取行

I'm trying to read a streaming data using PHP.

So I would like to read and show every message posted from a server in real time.

I assume that $uri, $context from the code snipped are correctly defined.

When I use this code, the only result I got is when then lenght is reached. It seams that it doesnt stop on delimiter "" string.

DEFINE('TOKEN', "MY_TOKEN");

$opts = array(
  'http' => array(
    'method' => "GET",
    'header'=> "Authorization: Basic " . base64_encode(TOKEN)));
$context = stream_context_create($opts);
if (($fp = fopen($uri, "r", false, $context))) {
  while (!feof($fp)) {
    $contents = stream_get_line($fp, 1024, "");
    echo "text: " . $contents . "
";
  }
fclose($fp);

When the lenght is reached, it show correctly all the new messages posted. with the string "text: " before every message.

How could I do to print one message by one ?

PS: I tried to use "", " ", " " or " ", nothing works :/.

I need some help there, thanks a lot.

Edit: adding the context

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类