0

我刚刚拿起 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];
  }
}
4

1 回答 1

2

将执行方法实现包装到 Amp\call() 以返回 Promise 而不是 Generator。然后在主循环上产生结果以获取数组而不是 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
} );
于 2021-04-16T07:53:49.040 回答