0

I've set up an account to try out and one of the most interesting part of their API is the webhooks. However, I haven't found a reference to how to 'catch' the webhooks using a PHP script. I imagine that it is something like:

<?php
//pseudo-ish code
$webhook = $_POST['webhook'];
$json = json_decode($webhook, true);

// code to save webhook data

Anyone have any ideas? Here is the link to their API

Per a comment, I tried:

<?php
$result = var_export($_POST, true);
$file = fopen("screenshots/test.txt","w");
echo fwrite($file, "testing:".$result);
fclose($file);
?>

and all that results is a file with the word "testing:array()" in it indicating that $_POST is empty.

Optimus
  • 1,354
  • 1
  • 21
  • 40
  • 1
    Why not `var_export` the entire `$_POST` to a file and analyze the request? As far as I see from the docs you don't need `json_decode` you just get a POST request with `$_POST['message']`, `$_POST['code']` etc. – Tony Bogdanov Feb 18 '16 at 16:43
  • I'll try that and report back. Thanks – Optimus Feb 18 '16 at 16:47
  • Updated the comment with my attempt and result. Still no joy. – Optimus Feb 18 '16 at 17:08
  • 1
    That's because `var_export` outputs the result instead of returning it. Should be `var_export($_POST, true)` ;) – Tony Bogdanov Feb 18 '16 at 17:12
  • now it produces testingarray(). So, $_POST is empty. – Optimus Feb 18 '16 at 17:15
  • So it leaves us with two possibilities, either it's a GET request, you can test that with `$_GET` or they're sending the payload in the body of the request, in this case try to `var_export` this: `stream_get_contents(STDIN)`. – Tony Bogdanov Feb 18 '16 at 17:22
  • $_GET and $_REQUEST both return empty arrays. stream_get_contents(STDIN) returns "false". – Optimus Feb 18 '16 at 17:27
  • 1
    Now that's just weird, could you dump your entire `$_SERVER` and share it here? – Tony Bogdanov Feb 18 '16 at 17:32

1 Answers1

2

The API sends the payload as a JSON encoded string in the body of the request.

$data = json_decode(trim(file_get_contents('php://input'), '"\''), true);
Tony Bogdanov
  • 7,436
  • 10
  • 49
  • 80
  • To save someone the trouble when they run into this, the string returned has a leading and trailing apostrophe, which causes json_decode to return NULL. Subsequently, you need to do something like `substr(file_get_contents('php'://input),1,-1)` to get it to work. – Optimus Feb 18 '16 at 17:59