How do I create a webhook?

ines pelaez picture ines pelaez · Aug 23, 2017 · Viewed 22.1k times · Source

So I am creating a click2call app, using Tropo and Nexmo, and at this point, I need help setting a webhook. They both provide a place to point a webhook, but now, I don't understand what to include there. Is it a php file? or a json? I've already tried to create a php and had the following:

<?php
    header('Content-Type: application/json');
    ob_start();
    $json = file_get_contents('php://input'); 
    $request = json_decode($json, true);
    $action = $request["result"]["action"];
    $parameters = $request["result"]["parameters"];
    $output["contextOut"] = array(array("name" => "$next-context", "parameters" =>
    array("param1" => $param1value, "param2" => $param2value)));
    $output["speech"] = $outputtext;
    $output["displayText"] = $outputtext;
    $output["source"] = "whatever.php";
    ob_end_clean();
    echo json_encode($output);
?>

how can I later retrieve the information from my webhook and store it in a DB? I've seen pieces of code, but I have no idea where to include it... is it in my api, in the php file that my webhook points?? Thank you in advance.

Answer

ines pelaez picture ines pelaez · Aug 23, 2017

So I figured it out, in a very simple way. Just point your webhook to a php with the following code:

<?php

header('Content-Type: application/json');
$request = file_get_contents('php://input');
$req_dump = print_r( $request, true );
$fp = file_put_contents( 'request.log', $req_dump );
?>

And the information will be posted and then acessible through the 'request.log'

Hope it can help others in the future.