2011-04-06 89 views
1

我有需要返回的数据后关闭连接的PHP脚本,但继续执行:的fsockopen并关闭在PHP连接,但继续执行脚本

<?php 
    // Some code 

    ob_start(); 

    echo $data; 

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

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

    // Close connection 

    // Some code, continue executing 
?> 

这是一个与一些客户的工作,但我需要有时打电话与其它PHP脚本

<?php 
    // Some code 

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

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

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

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

    // some code 
?> 

的问题是:接收数据时的fsockopen不紧密连接,只有当所有的第一脚本结束。

我唯一的想法是,检查数据收到时的长度和关闭手册。

回答

0

您的第一个(服务器)脚本将保持套接字打开,直到它终止。 套接字比HTTP低,当你发送Connection:close header时,你应该自己处理它,而不是fsockopen函数。

做你所做的事(计算响应的字节数)是正确的方式来处理它。