Skip to content
Advertisement

Empty Response from a webhook using php

When a new record is created or updated, the Webhook is triggered and it posts three keys in the post body. One of the keys named payload contains encoded JSON with attributes of the record.

This is an example of the POST body message:See the Payload here

I want when the webhook fires it creates a file and posts the date to the file but I’m geting empty payload.

Here is the code.

    <?php
/**
 * Created by PhpStorm.
 * User: Lee N
 * Date: 16/07/2018
 * Time: 14:46
 */


$data = file_get_contents('php://input');
//decode JSON data to PHP array
$content = json_decode($data, true);

if($content ==""){
    $data = "Payload fired but no datkhkjhkjhjka";

}else{
    $data = $content;

}

$pagename = 'from_vend';

$newFileName = $pagename.".txt";

if (file_put_contents($newFileName, $data) !== false) {
    echo "File created (" . basename($newFileName) . ")";
} else {
    echo "Cannot create file (" . basename($newFileName) . ")";
}

Advertisement

Answer

Try using parse_str($str, $output); instead of json_decode($data, true);

the post request is x-www-form-urlencoded. So json_decode will not work here as it is not a json string. If we check the payload we can see that it is a query string and parse_str will parse it into variables.

so the code will be

<?php
/**
* Created by PhpStorm.
* User: Lee N
* Date: 16/07/2018
* Time: 14:46
*/


$data = file_get_contents('php://input');
//decode JSON data to PHP array
//$content = json_decode($data, true);
parse_str($data, $content );
if($content ==""){
  $data = "Payload fired but no datkhkjhkjhjka";

}else{
  $data = $content;

}

$pagename = 'from_vend';

$newFileName = $pagename.".txt";

if (file_put_contents($newFileName, $data) !== false) {
  echo "File created (" . basename($newFileName) . ")";
} else {
  echo "Cannot create file (" . basename($newFileName) . ")";
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement