2017 © Pedro Peláez
 

library captainhook

Add webhooks to your Laravel app.

image

mpociot/captainhook

Add webhooks to your Laravel app.

  • Thursday, December 8, 2016
  • by mpociot
  • Repository
  • 10 Watchers
  • 295 Stars
  • 5,777 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 24 Forks
  • 7 Open issues
  • 26 Versions
  • 1 % Grown

The README.md

Captain Hook, (*1)

Captain Hook

Add Webhooks to your Laravel app, arrr

image image codecov.io Scrutinizer Code Quality Build Status StyleCI, (*2)

Implement multiple webhooks into your Laravel app using the Laravel Event system., (*3)

A webhook is a method of altering the behavior of a web application, with custom callbacks.
These callbacks may be managed by third-party users and developers who may not necessarily
be affiliated with the originating application.

Examples

php artisan hook:add http://www.myapp.com/hooks/ '\App\Events\PodcastWasPurchased'
php artisan hook:add http://www.myapp.com/hooks/ 'eloquent.saved: \App\User'
Webhook::create([
    "url" => Input::get("url"),
    "event" => "\\App\\Events\\MyEvent",
    "tenant_id" => Auth::id()
]);

Contents

, (*4)

Installation

In order to add CaptainHook to your project, just add, (*5)

"mpociot/captainhook": "~2.0"

to your composer.json's require block. Then run composer install or composer update., (*6)

Or run composer require mpociot/captainhook if you prefer that., (*7)

Then in your config/app.php add, (*8)

Mpociot\CaptainHook\CaptainHookServiceProvider::class

to the providers array., (*9)

Publish and run the migration to create the "webhooks" table that will hold all installed webhooks., (*10)

php artisan vendor:publish --provider="Mpociot\CaptainHook\CaptainHookServiceProvider"

php artisan migrate

, (*11)

Usage

The CaptainHook service provider listens for any eloquent.* events., (*12)

If the package finds a configured webhook for an event, it will make a POST request to the specified URL., (*13)

Webhook data is sent as JSON in the POST request body. The full event object is included and can be used directly, after parsing the JSON body., (*14)

Example, (*15)

Let's say you want to have a webhook that gets called every time your User model gets updated., (*16)

The event that gets called from Laravel will be:, (*17)

eloquent.updated: \App\User, (*18)

So this will be the event you want to listen for., (*19)

, (*20)

Add new webhooks

If you know which event you want to listen to, you can add a new webhook by using the hook:add artisan command., (*21)

This command takes two arguments:, (*22)

  • The webhook URL that will receive the POST requests
  • The event name. This could either be one of the eloquent.* events, or one of your custom events.
php artisan hook:add http://www.myapp.com/hook/ 'eloquent.saved: \App\User'

You can also add multiple webhooks for the same event, as all configured webhooks will get called asynchronously., (*23)

, (*24)

Delete existing webhooks

To remove an existing webhook from the system, use the hook:delete command. This command takes the webhook ID as an argument., (*25)

php artisan hook:delete 2

, (*26)

List all active webhooks

To list all existing webhooks, use the hook:list command., (*27)

It will output all configured webhooks in a table., (*28)

, (*29)

Spark

Install this package like stated in the Installation section, then follow the Spark installation instructions., (*30)

, (*31)

Custom event listeners

All listeners are defined in the config file located at config/captain_hook.php., (*32)

, (*33)

Receiving a webhook notification

To receive the event data in your configured webhook, use:, (*34)

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);

// Do something with $event_json

, (*35)

Webhook logging

Starting with version 2.0, this package allows you to log the payload and response of the triggered webhooks., (*36)

NOTE: A non-blocking queue driver (not sync) is highly recommended. Otherwise your application will need to wait for the webhook execution., (*37)

You can configure how many logs will be saved per webhook (Default 50)., (*38)

This value can be modified in the configuration file config/captain_hook.php., (*39)

, (*40)

Using webhooks with multi tenancy

Sometimes you don't want to use system wide webhooks, but rather want them scoped to a specific "tenant". This could be bound to a user or a team., (*41)

The webhook table has a field tenant_id for this purpose. So if you want your users to be able to add their own webhooks, you won't use the artisan commands to add webhooks to the database, but add them on your own., (*42)

To add a webhook that is scoped to the current user, you could do for example:, (*43)

Webhook::create([
    "url" => Input::get("url"),
    "event" => "\\App\\Events\\MyEvent",
    "tenant_id" => Auth::id()
]);

Now when you fire this event, you want to call the webhook only for the currently logged in user., (*44)

In order to filter the webhooks, modify the filter configuration value in the config/captain_hook.php file. This filter is a Laravel collection filter., (*45)

To return only the webhooks for the currently logged in user, it might look like this:, (*46)

'filter' => function( $webhook ){
    return $webhook->tenant_id == Auth::id();
},

, (*47)

License

CaptainHook is free software distributed under the terms of the MIT license., (*48)

'Day 02: Table, Lamp & Treasure Map' image licensed under Creative Commons 2.0 - Photo from stevedave, (*49)

The Versions

08/12 2016
25/05 2016

dev-analysis-zDyAy9

dev-analysis-zDyAy9 http://github.com/mpociot/captainhook

Add webhooks to your Laravel app.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel hook events webhook webhooks

16/04 2016

dev-analysis-zGDLMg

dev-analysis-zGDLMg http://github.com/mpociot/captainhook

Add webhooks to your Laravel app.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel hook events webhook webhooks

09/03 2016

dev-analysis-q2QDlB

dev-analysis-q2QDlB http://github.com/mpociot/captainhook

Add webhooks to your Laravel app.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel hook events webhook webhooks

09/03 2016

dev-analysis-qvQGeN

dev-analysis-qvQGeN http://github.com/mpociot/captainhook

Add webhooks to your Laravel app.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel hook events webhook webhooks

17/02 2016

dev-analysis-qgO15w

dev-analysis-qgO15w http://github.com/mpociot/captainhook

Add webhooks to your Laravel app.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel hook events webhook webhooks

16/02 2016

dev-analysis-z9mPL6

dev-analysis-z9mPL6 http://github.com/mpociot/captainhook

Add webhooks to your Laravel app.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel hook events webhook webhooks

15/02 2016

dev-analysis-q1blmN

dev-analysis-q1blmN http://github.com/mpociot/captainhook

Add webhooks to your Laravel app.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel hook events webhook webhooks