我正在测试doc中的curl_multi_await示例(请参阅下面的代码),但我无法得到任何响应。 我使用的是hhvm版本3.8.1。
我遇到了HH \ Asio \ curl_exec的问题,但它已通过更新hhvm修复,是否相关?
<?hh
async function curl_exec_await(string $url): Awaitable<string> {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch);
do {
$active = 1;
do {
$status = curl_multi_exec($mh, $active);
} while ($status == CURLM_CALL_MULTI_PERFORM);
$select = await curl_multi_await($mh);
if ($select == -1) break;
} while ($status === CURLM_OK);
$content = (string)curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
return $content;
}
var_dump(curl_exec_await('http://example.com')->getWaitHandle()->join());
此代码打印一个空字符串。
感谢您的帮助!
答案 0 :(得分:0)
\HH\Asio\curl_exec
的形式提供,您只需使用它而不是curl_exec_await
。见the docs。答案 1 :(得分:0)
以下是修复的示例,如果它可以帮助某人:
<?hh
async function curl_exec_await(string $url): Awaitable<string> {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch);
$active = 0;
do {
$status = curl_multi_exec($mh, $active);
await RescheduleWaitHandle::create(
RescheduleWaitHandle::QUEUE_DEFAULT,
0,
);
} while ($status == CURLM_CALL_MULTI_PERFORM || $active > 0);
await curl_multi_await($mh);
$content = (string)curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
curl_multi_close($mh);
return $content;
}
var_dump(curl_exec_await('http://example.com')->getWaitHandle()->join());