ines pelaez
ines pelaez

Reputation: 523

How do I create a webhook?

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.

Upvotes: 19

Views: 56273

Answers (2)

ines pelaez
ines pelaez

Reputation: 523

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

<?php
// Original Answer
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 );

// Updated Answer
if($json = json_decode(file_get_contents("php://input"), true)){
   $data = $json;
}
print_r($data);
?>

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

Hope it can help others in the future.

Upvotes: 27

Twisted1919
Twisted1919

Reputation: 2499

A webhook url is a place on your server where the above providers will send data from time to time when something happens and you need to know about, for example you might get a request each time a sms is sent, or each time a sms fails sending and based on that info you can take further actions, like marking the fact that user phone number isn't valid anymore.

Let's say your webhook url is something like https://www.yoursite.com/webhooks.php which means in your webhooks.php file you have to place some PHP code that will read the incoming request and will do something with the information that it contains.

Upvotes: 14

Related Questions