dqyz48470 2011-04-06 08:26
浏览 77

fsockopen&在PHP中关闭连接但继续执行脚本

I've a PHP script that need Close connection after return data, but continue executing:

<?php
  // Some code

  ob_start();

  echo $data;

  header("Content-type: application/soap+xml;charset=UTF-8
");
  header("Content-Length: " . ob_get_length() . "
");
  header("Content-Encoding: none
");
  header("Connection: close

");

  // Print buffer
  ob_end_flush();
  ob_flush();
  flush();

  // Close connection

  // Some code, continue executing
?>

It's work with some clients, but I need to call sometimes with an other PHP script

<?php
    // Some code

    $connection = @fsockopen($url['host'], $url['port'], $errno, $errstr);

    if (!$connection) { 
      throw new Exception('');
    }

    fputs($connection, "POST {$url['path']} HTTP/1.1
");
    fputs($connection, "Host: {$url['host']}
");
    fputs($connection, "Content-type: text/xml;charset=UTF-8
");
    fputs($connection, "SOAPAction: \"{$soapaction}\"
");
    fputs($connection, "Content-Length: " . strlen($request) . "
");
    fputs($connection, "Content-Encoding: none
");
    fputs($connection, "Connection: close

");
    fputs($connection, $request);

    $respponse = '';
    while(!feof($connection)) {
      // receive the results of the request
      $response .= fgets($connection, 512);
    }

    // some code
?>

The problem is: fsockopen doesn't close connection when receive the data, only when all first scripts ends.

The only idea I have, it is check the length and close manual when the data is received.

  • 写回答

1条回答 默认 最新

  • duanque19820925 2011-05-07 09:54
    关注

    Your first (server) script will keep the socket open untill it terminates. Sockets are lower level then HTTP and when you send Connection: close header you second (client) script is supposed to handle it itself rather then fsockopen function.

    Doing what you do (counting number of bytes in response) is the right way to deal with it.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大