Slack PHP API - 避免超时错误

Vim*_*ath 10 php curl timeout slack-api slack

我正在尝试使用Slack Custom命令,并且不太确定如何使用延迟消息,因为Yoda Speak External API需要3秒以上的响应时间.

我做了以下事情:

  • /Yoda在我的情况下发送了slack命令并收到了reponse_url.
  • post对响应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);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 现在,当我调用Yoda API时,它会出现以下错误"已达到超时".我读到延迟回复,但不知道我应该怎样从这里开始.
$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;
Run Code Online (Sandbox Code Playgroud)

有人可以让我知道如何使用延迟响应摆脱超时错误?

更新的代码:

$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);
Run Code Online (Sandbox Code Playgroud)

我仍然得到相同的错误"Darn - 斜杠命令不起作用(错误消息:) Timeout was reached.在slash-command管理命令"

rco*_*oup 6

你正在做所有正确的事情,只需要改变秩序.

  1. 立即响应原始请求并200 OK做出响应.有关详细信息,请参阅此答案,但基本上

    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();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后使用curl制作Yoda API请求,就像你正在做的那样

  3. 一旦你有了Yoda结果$response_url,就像你正在做的那样,用curl 将它们发送给Slack .