Slack PHP API - 避免超时错误 [英] Slack PHP API - Avoid Timeout error

查看:344
本文介绍了Slack PHP API - 避免超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Slack Custom命令,但不知道自




  • 现在,当我调用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;


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



更新代码

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

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

//https://paypal.slack.com/services/B0VQMHX8W#service_setup
//初始以200OK响应超时
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);

我仍然得到相同的错误Darn - slash命令无效(错误信息:管理命令at slash-command

解决方案


  1. 使用<$回应原始要求c $ c> 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();


  2. 然后使用curl进行Yoda API请求're doing


  3. 一旦您有Yoda结果,请使用curl将它们发送到Slack, $ response_url


I am trying to use Slack Custom command and not pretty sure how to use delayed messages since the Yoda Speak External API takes more than 3 seconds to respond.

I have done the following:

  • Sent the slack command /Yoda in my case and received the reponse_url.
  • Used the following to post the following to the response 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);

  • Now, when I call the Yoda API, it gives the following error "Timeout was reached". I read about delayed responses but not sure how should I proceed from here.

$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;

Can someone please let me know how to get rid of the timeout error using delayed responses?

UPDATED CODE:

$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);

I still get the same error "Darn – that slash command didn't work (error message: Timeout was reached). Manage the command at slash-command"

解决方案

You're doing all the right things, just need to change the order.

  1. Respond to the original request with a 200 OK response immediately. See this answer for details, but essentially:

    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. Then make the Yoda API request using curl, as you're doing

  3. Once you have the Yoda results, send them to Slack at $response_url using curl, as you're doing.

这篇关于Slack PHP API - 避免超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆