2011-04-06 160 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函數。

做你所做的事(計算響應的字節數)是正確的方式來處理它。