我刚刚获得了AmPHP,我正在尝试从我的AmPHP http服务器获取post正文,但是,它一直在运行(只是从来没有向我的客户端发送回复)。
这是我目前使用的代码:
$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];
}
}发布于 2021-04-16 07:53:49
将execute方法实现包装为Amp\call(),以返回诺言,而不是生成器。然后在主循环上产生结果,得到数组而不是null。
// Webhook
class Webhook {
public function execute( \Amp\Http\Server\Request $request ) {
return Amp\call( function () use ( $request ) {
$postbody = yield $request->getBody()->buffer();
return [ '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( $resp ); // array with post body
} );https://stackoverflow.com/questions/66161838
复制相似问题