php - Slack PHP API - 避免超时错误

标签 php curl timeout slack-api slack

我正在尝试使用 Slack 自定义命令,但不太确定如何使用自 Yoda Speak 以来的延迟消息外部 API 响应时间超过 3 秒。

我做了以下事情:

  • 在我的案例中发送了 slack 命令 /Yoda 并收到了 reponse_url
  • 使用以下内容将以下内容发布到响应 URL。
$data_string = '{"response_type": "in_channel", "text":"Checking,please wait..."}' ;
$chs = curl_init();
curl_setopt($chs, CURLOPT_URL, $response_url);
curl_setopt($chs, CURLOPT_POST, true);
curl_setopt($chs, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($chs, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($chs, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($chs, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chs, CURLOPT_POST, 1);
curl_setopt($chs, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$results = curl_exec($chs);

enter image description here

  • 现在,当我调用 Yoda API 时,它给出以下错误“已达到超时”。我读到了 delayed responses但不确定我应该如何从这里开始。
$chsres = curl_init();
curl_setopt($chsres, CURLOPT_URL, "https://yoda.p.mashape.com/yoda?sentence=welcome+to+stack");
curl_setopt($chsres, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($chsres, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($chsres, CURLOPT_VERBOSE, true);
curl_setopt($chsres, CURLOPT_TIMEOUT, 45);
curl_setopt($chsres, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chsres, CURLOPT_HTTPHEADER, array('Content-Type:application/json', "X-Mashape-Key:> deMeGoBfMvmshQSemozTqJEY9z0jp1eIhuAjsnx9cQAQsHUifD"));
$resultchsres = curl_exec($chsres);
echo $resultchsres;

有人可以告诉我如何使用延迟响应来消除超时错误吗?

更新代码:

$response_url = $_POST['response_url'];
$text = $_POST['text'];

$term = str_replace(' ', '+', $text);

//https://paypal.slack.com/services/B0VQMHX8W#service_setup
//initial respond with 200OK for timeout
ignore_user_abort(true);
set_time_limit(0);
ob_start();
echo('{"response_type": "in_channel", "text": "Checking, please wait..."}');
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Content-Type: application/json");
header('Content-Length: '.ob_get_length());
ob_end_flush();
ob_flush();
flush();


    $chsres = curl_init();
    curl_setopt_array($chsres, array(
        CURLOPT_URL => "https://yoda.p.mashape.com/yoda?sentence=$term",
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_SSL_VERIFYHOST => FALSE,
        CURLOPT_VERBOSE => true,
        CURLOPT_RETURNTRANSFER => FALSE,
        CURLOPT_HTTPHEADER => array('Content-Type:application/json', "X-Mashape-Key: deMeGoBfMvmshQSemozTqJEY9z0jp1eIhuAjsnx9cQAQsHUifD"),
        CURLOPT_RETURNTRANSFER => true
    ));
    $yodaresponse = curl_exec($chsres);

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $response_url,
        CURLOPT_POST => 1,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $yodaresponse
    ));

    $resp = curl_exec($curl);
    var_dump($resp);
    curl_close($curl);

我仍然收到相同的错误“该死的 - 斜杠命令不起作用(错误消息:超时)。在斜杠命令中管理命令”

最佳答案

您正在做所有正确的事情,只需要更改顺序即可。

  1. 立即以 200 OK 响应响应原始请求。参见 this answer有关详细信息,但本质上:

    ignore_user_abort(true);
    ob_start();
    echo('{"response_type": "in_channel", "text": "Checking, please wait..."}');
    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
    header("Content-Type: application/json");
    header('Content-Length: '.ob_get_length());
    ob_end_flush();
    ob_flush();
    flush();
    
  2. 然后像您一样使用 curl 发出 Yoda API 请求

  3. 获得 Yoda 结果后,使用 curl 将它们发送到 $response_url 的 Slack,正如您正在做的那样。

关于php - Slack PHP API - 避免超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195924/

相关文章:

php - Twig 动态变量调用

php - Laravel 家园 : How to fix 'cURL error 60: SSL certificate problem'

php - Azure AD 身份验证问题 - 不记名 token 仅返回应用程序信息

java - 在固定时间后中断线程,是否必须抛出 InterruptedException?

c# - 实现 C# 通用超时

mysql - 由于许多连接错误,主机 'X' 被阻止

php - 在 PHP 中处理二进制数据

以下划线开头的 PHP SOAP 操作

php - 如何在启用 JavaScript 的情况下进行 cURL?

php: DateTime() 和 time() 有不同的时间