7

I want to handle data, that is sended by a trello webhook. There for the webhook posts to a url like site.com/tracker.php

In the tracker.php I want to save the data in a database. For that I need to get some params.

This is a example of the code I receive (https://trello.com/docs/gettingstarted/webhooks.html):

{
   "action": {
      "id":"51f9424bcd6e040f3c002412",
      "idMemberCreator":"4fc78a59a885233f4b349bd9",
      "data": {
         "board": {
            "name":"Trello Development",
            "id":"4d5ea62fd76aa1136000000c"
         },
         "card": {
            "idShort":1458,
            "name":"Webhooks",
            "id":"51a79e72dbb7e23c7c003778"
         },
         "voted":true
      },
      "type":"voteOnCard",
      "date":"2013-07-31T16:58:51.949Z",
      "memberCreator": {
         "id":"4fc78a59a885233f4b349bd9",
         "avatarHash":"2da34d23b5f1ac1a20e2a01157bfa9fe",
         "fullName":"Doug Patti",
         "initials":"DP",
         "username":"doug"
      }
   },
   "model": {
      "id":"4d5ea62fd76aa1136000000c",
      "name":"Trello Development",
      "desc":"Trello board used by the Trello team to track work on Trello.  How meta!\n\nThe development of the Trello API is being tracked at https://trello.com/api\n\nThe development of Trello Mobile applications is being tracked at https://trello.com/mobile",
      "closed":false,
      "idOrganization":"4e1452614e4b8698470000e0",
      "pinned":true,
      "url":"https://trello.com/b/nC8QJJoZ/trello-development",
      "prefs": {
         "permissionLevel":"public",
         "voting":"public",
         "comments":"public",
         "invitations":"members",
         "selfJoin":false,
         "cardCovers":true,
         "canBePublic":false,
         "canBeOrg":false,
         "canBePrivate":false,
         "canInvite":true
      },
      "labelNames": {
         "yellow":"Infrastructure",
         "red":"Bug",
         "purple":"Repro'd",
         "orange":"Feature",
         "green":"Mobile",
         "blue":"Verified"
      }
   }
}

And this is my current tracker.php file:

<?php

$json = $_POST["actions"];
$action = json_decode($json);
$action_id = $action->id;
$card_id = $action->data->card->id;
var_dump($array);

My questions:

  • Is the $_POST["actions"] right? Or what do I need inside the []
  • Is the way I want to get the $action->data->card->id right?
  • Is there any way to see the result of the var_dump? Dont know how to see the result of an webhook post..

3 Answers 3

19

I had to use this:

$json = file_get_contents('php://input');
$action = json_decode($json, true);

As far as I understand the json request is not automaticly split into the $_POST. Thus you have to use the input itself.

The true-parameter in json_decode is needed to get an associative array. Without it I only got an empty array.

3
  • What about security? How to be sure if response is sent by trello only? Dec 8, 2017 at 6:36
  • @DavinderKumar that's a different question. You should ask that seperatly and provide enough information about it.
    – DocRattie
    Jan 31, 2018 at 10:08
  • @DavinderKumar I think we could get that from $_SERVER['REMOTE_HOST'] (if it's set, else deny) and verify it.
    – nice_dev
    Nov 6, 2018 at 13:25
2

You can use this to check if the received data is a JSON formatted or not.

if($json = json_decode(file_get_contents("php://input"), true)) {
    print_r($json);
    $data = $json;
} else {
    print_r($_POST);
    $data = $_POST;
}
0

in laravel with php8>

in routes/api.php:

            Route::middleware(['checkPublicServiceTokens'])
                ->group(function () {
                    Route::post('/webhook', [
                        WhatsAppController::class,
                        'webhook'
                    ])->name('whatsapp.webhook');
                });

in app/http/kernel.php in routeMiddleware section:

protected $routeMiddleware = [

add this:

    'checkPublicServiceTokens' => \App\Http\Middleware\CheckPublicServiceTokens::class,

in app/http/Middleware:

public function handle(Request $request, Closure $next)
{
    if (!$request->input('public-webhook-token') || $this->isTokenValid($request)) {
        return apiResponse()->errors(__('exception.access_denied_public'))
            ->status(Response::HTTP_UNAUTHORIZED)
            ->get();
    }

    return $next($request);
}

in lang/en/exception.php

'access_denied_public' => 'authorized token as webhook token',

in your controller: public function webhook(WhatsAppWebhookRequest $request): JsonResponse { $item = app()->make(WhatsAppWebhookRepository::class)->store($request->all()); return apiResponse()->data($item)->status(Response::HTTP_CREATED)->get(); }

in store method in your implementation :

public function store(array $data): WhatsAppWebhook
{
    $payload = array("payload"=>json_encode($data));
    $item = new WhatsAppWebhook($payload);
    $item->save();
    return $item;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.