使用ReactPHP异步进行长轮询电报

问题描述 投票:1回答:1

我正在尝试对reactphp进行长时间轮询。我有一个函数getNotifications可以长时间轮询,以等待Telegram的响应。该电报API可以将请求保持打开状态,直到timeout或如果有通知,可以在50秒结束时<发送响应。在收到Telegram的回复后,我该如何回忆起getNotifications?基本上,我希望在有响应时再次调用getNotifications。这是我的代码,谢谢大家<?php require "vendor/autoload.php"; use Clue\React\Buzz\Browser; use Psr\Http\Message\ResponseInterface; use React\EventLoop\LoopInterface; $loop = React\EventLoop\Factory::create(); $browser = new Browser($loop); $method = "getUpdates"; $timeout = 50; $params = [ "offset" => 550495530, "limit" => 1000, "timeout" => $timeout ]; $bot_key = "your bot token"; $query = http_build_query($params); $url = "https://api.telegram.org/bot" . $bot_key . "/" . $method . "?" . $query; $browser = $browser->withOptions(array( 'timeout' => $timeout )); function callback(){ echo "done"; } function getNotifications($url, $browser, LoopInterface $loop){ $browser->get($url)->then(function (ResponseInterface $response) { // response received within 50 seconds. Telegram longpolling var_dump((string)$response->getBody()); callback(); }); } function timer($start, LoopInterface $loop) { //Timer only used to count seconds $loop->addPeriodicTimer(1.0, function ($timer) use (&$start, $loop) { echo "tick ". $start++ . "\n"; }); } timer(0, $loop); getNotifications($url, $browser, $loop); $loop->run();

我正在尝试使用reactphp进行长时间轮询。我有一个功能getNotifications,它保持长时间轮询,等待Telegram的响应。该电报API可以保持请求打开,直到...
php asynchronous telegram-bot long-polling reactphp
1个回答
0
投票
好吧,我找到了解决方案。要将参数传递给匿名函数,我需要使用

use

© www.soinside.com 2019 - 2024. All rights reserved.