我刚刚拿起 AmPHP,并尝试从我的 AmPHP http 服务器获取帖子正文,但是,它会永远持续下去(只是永远不会向我的客户端发回响应)。
这是我当前正在使用的代码:
$resp = \Amp\Promise\wait($request->getBody()->buffer());
我测试了另一段代码,它不会永远持续下去,但是当我使用那段代码时,我无法将我的身体移到
onResolve
中的函数之外:
$resp = $request->getBody()->buffer()->onResolve(function($error, $value) {
return $value;
});
return $resp; // returns null
我也尝试过最后一点,但这也只是返回
null
:
return yield $request->getBody()->buffer();
编辑:做了一些更多的摆弄,这是我当前的(仍然不起作用)代码(尽管为了简单起见,很多内容已经被删除):
// Main loop
Loop::run(function() {
$webhook = new Webhook();
$resp = $webhook->execute($request);
print_r($resp); // null
});
// Webhook
class Webhook {
public function execute(\Amp\Http\Server\Request $request) {
$postbody = yield $request->getBody()->buffer();
return ['success' => true, 'message' => 'Webhook executed successfully', 'data' => $postbody];
}
}
更新:AMPHP v3 发布后(变更日志)代码将如下所示:
use Amp\Http\Server\Request;
use Amp\Loop;
class Webhook {
public function execute(Request $request): \Amp\Promise {
return new \Amp\Promise(function (callable $resolve) use ($request) {
$postbody = yield $request->getBody()->buffer();
$resolve([ 'success' => true, 'message' => 'Webhook executed successfully', 'data' => $postbody ]);
});
}
}
// Main loop
Loop::run(function () {
$webhook = new Webhook();
$resp = $webhook->execute($request);
$output = yield $resp;
print_r($output); // array with post body
});
将execute方法实现包装到Am中