我需要测试我的PHP脚本是否正确地处理了我的GUSICE请求中的READ_TIMEOUT选项.所以我写了两个PHP脚本,一个用GUZLE调用另一个,我想用这种方式来实现超时.但出于某种原因,我没有得到暂停.该文档指出,只有当stream设置为True和使用CurlHandler时,超时才起作用,但我已经验证了这两种情况.可能是我的服务器部分做得不好,所以真的没有发送数据?或者为什么超时不起作用?我有几个版本的脚本,现在我使用这个版本. 客户端:
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
$client = new Client(["read_timeout"=> 2, "stream" => true]);
try {
$response = $client->request('GET', 'http://localhost:8002/server_test.php', [
'stream' => true,
'read_timeout' => 2 // Just the read timeout
]);
$body = $response->getBody();
while (!$body->eof()) {
echo $body->read(1024);
}
} catch (RequestException $e) {
echo "Error: " . $e->getMessage();
}
?>
服务器端:
<?php
// Stream the response
$stream = fopen('php://output', 'w');
$start_time = time();
// Stream data with delay
for ($i = 0; $i < 3; $i++) {
fwrite($stream, "Data chunk $i\n");
sleep(6);
}
$data = "\nStart time: $start_time\nEnd time: " . time();
fwrite($stream, $data);
fclose($stream);
?>
客户端脚本正在调用服务器脚本,应该会超时,因为服务器脚本在发送数据块之间等待6秒.然而,没有发生超时.