Laravel spatie webhook 客户端 - Handle 方法不处理数据

问题描述 投票:0回答:1

我最近需要使用 webhooks 在我的 laravel 应用程序中接收信息,我已经实现了 spatie/webhook-client,但是在 ProcessWebhookJob -> Handle 方法中没有任何作用。

更正:我的意思是,即使到达“Handle”方法,也没有执行任何代码,就像被忽略一样......

我的 webhook-client.php

return [
    'configs' => [
        [
            /*
             * This package supports multiple webhook receiving endpoints. If you only have
             * one endpoint receiving webhooks, you can use 'default'.
             */
            'name' => 'nagios-webhook',

            /*
             * We expect that every webhook call will be signed using a secret. This secret
             * is used to verify that the payload has not been tampered with.
             */
            'signing_secret' => env('WEBHOOK_CLIENT_SECRET'),

            /*
             * The name of the header containing the signature.
             */
            'signature_header_name' => 'Signature',

            /*
             *  This class will verify that the content of the signature header is valid.
             *
             * It should implement \Spatie\WebhookClient\SignatureValidator\SignatureValidator
             */
            'signature_validator' => \Spatie\WebhookClient\SignatureValidator\DefaultSignatureValidator::class,

            /*
             * This class determines if the webhook call should be stored and processed.
             */
            'webhook_profile' => \Spatie\WebhookClient\WebhookProfile\ProcessEverythingWebhookProfile::class,

            /*
             * This class determines the response on a valid webhook call.
             */
            'webhook_response' => \Spatie\WebhookClient\WebhookResponse\DefaultRespondsTo::class,

            /*
             * The classname of the model to be used to store webhook calls. The class should
             * be equal or extend Spatie\WebhookClient\Models\WebhookCall.
             */
            'webhook_model' => \Spatie\WebhookClient\Models\WebhookCall::class,

            /*
             * In this array, you can pass the headers that should be stored on
             * the webhook call model when a webhook comes in.
             *
             * To store all headers, set this value to `*`.
             */
            'store_headers' => [

            ],

            /*
             * The class name of the job that will process the webhook request.
             *
             * This should be set to a class that extends \Spatie\WebhookClient\Jobs\ProcessWebhookJob.
             */
            'process_webhook_job' => App\Handler\ProcessWebhookJob::class,
        ]
    ],
];

我的 ProcessWebhookJob.php

<?php

namespace App\Handler;

//use \Spatie\WebhookClient\ProcessWebhookJob;
use Spatie\WebhookClient\Jobs\ProcessWebhookJob as SpatieProcessWebhookJob;
//use Illuminate\Support\Facades\Log;
use App\Models\User;
//use Log;

//The class extends "ProcessWebhookJob" class as that is the class //that will handle the job of processing our webhook before we have //access to it.
//class ProcessWebhook extends ProcessWebhookJob
class ProcessWebhookJob extends SpatieProcessWebhookJob
{
    public function handle()
    {
        $data = json_decode($this->webhookCall, true)['payload'];
        //Do something with the event
        //log($data);
        User::create([
                    'nombre'        => 'prueba1'
                ]);
        http_response_code(200); //Acknowledge you received the response
    }
}

尚未创建用户。

我还禁用了csrf验证:

验证CsrfToken.php

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'monitor-webhook',
    ];
}

这是我的 api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

/*Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});*/

Route::apiResource('v1/gps', App\Http\Controllers\Api\V1\GpsController::class)->middleware('api');

Route::webhooks('monitor-webhook', 'monitor-webhook')->middleware('api');

还有 kernel.php 文件


    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \Inspector\Laravel\Middleware\WebRequestMonitoring::class,
            \Spatie\Csp\AddCspHeaders::class,
            \App\Http\Middleware\SecurityHeaders::class,
        ],

        'api' => [
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \Inspector\Laravel\Middleware\WebRequestMonitoring::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'tenant.web' => [
            \Spatie\Multitenancy\Http\Middleware\NeedsTenant::class,
            \Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession::class,
        ],

        'tenant.api' => [
            \Spatie\Multitenancy\Http\Middleware\NeedsTenant::class, 
        ],
    ];

谢谢,

php laravel api webhooks
1个回答
0
投票

您的 Webhook 处理程序类默认扩展 ShouldQueue,您的有效负载将在 .env 文件中当前的 QUEUE_CONNECTION 中进行处理

QUEUE_CONNECTION=database 

因此,如果您需要处理当前的有效负载而不等待调度,您可以切换到

QUEUE_CONNECTION=sync 
© www.soinside.com 2019 - 2024. All rights reserved.