【发布时间】:2011-04-06 08:26:20
【问题描述】:
我有一个 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 接收数据时不会关闭连接,只有在所有第一个脚本结束时。
我唯一的想法,就是在收到数据时检查长度并关闭手册。
【问题讨论】:
标签: php http soap http-headers buffer