因此,我正在使用Xero API设置一个Webhook,它期望一个没有cookie和gzip等的空白响应。我似乎无法弄清楚如何发送一个完全空白的响应。
这是我对ngrok的回应的一个示例:
HTTP/1.1 401 Unauthorized
Server: nginx/1.13.3
Date: Wed, 12 Dec 2018 02:11:07 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
0
以下是执行HTTP响应的代码:
http_response_code(401);
exit;
我也尝试过:
return response(null, 401);
但是在webhook设置面板中,它显示了此错误:
Intent To Receive required
Last attempt at 2018-12-12 02:15:57 UTC
Failed to respond in timely manner
尽管响应时间<0.5s。我已经向Xero发送了许多屏幕录像,但是他们的支持似乎认为它会起作用。
答案 0 :(得分:2)
如错误所示,似乎在您的代码中您无法及时响应(5秒)。 请参阅此Failed to respond in timely manner issue 。 在使用laravel开发Xero集成时,我也遇到了这个问题。能够使用队列解决此问题,如果哈希匹配,我将Xero事件调度到作业,否则返回400。由于该事件正在队列中进行处理,它将及时返回响应。
use App\Jobs\XeroWebhook;
public function getUpdatedInvoiceInXero(Request $request)
{
$paylod = file_get_contents('php://input');
$events = json_decode($request->getContent())->events;
$XeroWebhookKey= "your_webhook_key";
$Hash = base64_encode(hash_hmac('sha256', $paylod, $XeroWebhookKey, true));
if ($Hash === $_SERVER['HTTP_X_XERO_SIGNATURE']) {
XeroWebhook::dispatch($events);
} else {
return response(null, 401);
}
}
正如您在这里看到的那样,我仅检查哈希匹配,在“ XeroWebhook”作业中包含了其他功能。 Laravel queues
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class XeroWebhook implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $events;
public function __construct($events, $tenantId)
{
$this->events = $events;
}
public function handle()
{
// rest of the code
}
}