Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有ReactPHP异步的长轮询电报_Php_Asynchronous_Telegram Bot_Long Polling_Reactphp - Fatal编程技术网

带有ReactPHP异步的长轮询电报

带有ReactPHP异步的长轮询电报,php,asynchronous,telegram-bot,long-polling,reactphp,Php,Asynchronous,Telegram Bot,Long Polling,Reactphp,我正在尝试使用reactphp进行长轮询。 我有一个函数getNotifications,它在长时间轮询中等待电报的响应 此电报api可将请求保持打开状态,直到超时 如果有通知,可以在50秒结束前发送响应。 收到电报回复后,我怎样才能回忆起收到的通知? 基本上,我希望在有响应时再次调用getNotifications 这是我的代码,谢谢大家 <?php require "vendor/autoload.php"; use Clue\React\Buzz\Browser; use Psr

我正在尝试使用reactphp进行长轮询。

我有一个函数getNotifications,它在长时间轮询中等待电报的响应

此电报api可将请求保持打开状态,直到超时 如果有通知,可以在50秒结束前发送响应。

收到电报回复后,我怎样才能回忆起收到的通知? 基本上,我希望在有响应时再次调用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();

好的,我找到了解决方案。要将参数传递给匿名函数,我需要使用use关键字。然后在函数内部,我可以正确地访问变量

function getNotifications($url, $browser, LoopInterface $loop){
    $browser->get($url)->then(function (ResponseInterface $response) use ($url, $browser,$loop) {
      // response received within 50 seconds. Telegram longpolling
      var_dump((string)$response->getBody());
      callback();
      getNotifications($url, $browser,$loop);
});

}好的,我找到了解决办法。要将参数传递给匿名函数,我需要使用use关键字。然后在函数内部,我可以正确地访问变量

function getNotifications($url, $browser, LoopInterface $loop){
    $browser->get($url)->then(function (ResponseInterface $response) use ($url, $browser,$loop) {
      // response received within 50 seconds. Telegram longpolling
      var_dump((string)$response->getBody());
      callback();
      getNotifications($url, $browser,$loop);
});

}

您是否检查cron作业?谢谢,但我不想使用cron作业。我想要一个带有事件循环的php文件,该文件以异步方式检查来自电报的更新,同时在我的代码中执行计时器等其他操作。是什么阻止您在回调后再次调用该函数?我没有访问$url、$browser、$loop的权限。您是否检查cron作业?谢谢,但我不想使用cron作业。我想要一个带有事件循环的php文件,该文件以异步方式检查来自电报的更新,同时在代码中执行计时器之类的其他操作。是什么阻止您在回调后再次调用该函数?我没有访问$url、$browser、$loop的权限