How Do
Webhooks
Webhooks

In order to implement a webhook you'll need a server of some sort.

If you haven't got access to a web host you can implement your own server on Windows using IIS which is free with many versions of Windows, or on Linux you can use Apache. On a Raspberry Pi I use Lighttpd which is simple and easy to use.


On your server you need to have PHP enabled. Once it's up and running you can create a PHP file. In this example the file is called 'webhooks.php'. The server is Lighttpd running on a Raspberry Pi at local address 192.168.2.29.

You can view or download all the files in this How Do here or clone the Git repository onto a Raspberry Pi or PC using this command:

git clone https://bitbucket.org/iotha/webhooks.git

Here at the start of the file we echo a message so that we can see the server has responded and we get the parameters that were passed to the PHP program and store them in local variables.

We also get the body of the HTTP call which we could use for our command as well.

When you use the webhook, not all the parameters have to be passed (although you could change the code to specify they do) and you can change these parameter names to whatever suits you.


Get the parameters
  <?php
      echo "Hello<br>";

      $device=$_GET["dev"];
      $command=$_GET["com"];
      $value=$_GET["val"];
      $key=$_GET["key"];
      $subdev=$_GET["sub"];

      $entityBody = file_get_contents('php://input');

Once we've got all the parameters they can be concatenated together, ready to send.

Concatenate the parameters
      $command = $command.",".$value.",".$key.",".$subdev.",".$device.",".$entityBody."\0";

Next we create a socket and connect to the machine that is going to handle our webhook. Here it is the Raspberry Pi again at address 192.168.2.29. The software that handles the webhook is listening in port 8090.You can have any address and port you want to handle the webhook. It can be a PC, a Raspberry Pi, ESP8266 or any other machine on the internet or your local network that you run your code on.

If the connection is successfully made the 'ONLINE' message will be displayed, if not we dislay an error message

Connect to the socket
      $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

      $connection =  @socket_connect($socket, '192.168.2.29', 8090);

      if( $connection )
      {
          echo 'ONLINE';
      }
      else
      {
          echo 'OFFLINE: ' . socket_strerror(socket_last_error( $socket ));
      }
      echo "<br>";

Now the parameters are sent to the webhook handling program using the socket.

Send the message
      $a = socket_write($socket, $command);

And the response is read back and displayed on the screen. The buffer size is 1024 bytes so we can get back a maximum of 1024 characters:

Read the response
      $read = socket_read( $socket, 1024 );
      if( $read == false )
      {
          echo "Socket read failed<br>";
      }
      else
      {
          echo $read;
      }

  ?>

That's it for the PHP code. Next we look at how to handle the webhook in your software.