函数名称:Swoole\Coroutine\Http\Client::recv()
适用版本:Swoole 2.0.0以上版本
函数说明:该函数用于从服务器接收响应数据。
用法:
string Swoole\Coroutine\Http\Client::recv(int $timeout = -1)
参数:
$timeout
(可选):设置超时时间,单位为秒,默认为-1,表示不设置超时时间。
返回值:
- 成功时,返回接收到的响应数据(字符串类型)。
- 失败时,返回false。
示例:
<?php
use Swoole\Coroutine\Http\Client;
function request($url)
{
$client = new Client($url['host'], $url['port']);
// 发起GET请求
$client->get($url['path']);
// 接收响应数据
$response = $client->recv();
if ($response === false) {
echo "Failed to receive response\n";
} else {
echo "Received response: " . $response . "\n";
}
$client->close();
}
$url = parse_url('http://www.example.com/');
request($url);
?>
以上示例中,我们创建了一个Swoole\Coroutine\Http\Client
对象,并使用get()
方法发送GET请求。然后,我们调用recv()
函数接收响应数据,并根据返回值判断接收是否成功。最后,关闭客户端连接。