Post navigation

Integrations, Product Information

Example code for PHP Webhook Endpoint

Every Webhook needs an endpoint — a listener script that “does something” when it receives a notification.

SurveyTown has survey webhooks that allow you to receive notifications when different events happen inside your SurveyTown account.  Why would you want to receive these notifications?  So a common use case would be if you wanted to update a separate systems when someone completed a survey, you could receive a webhook notification from SurveyTown when the survey was completed and after receiving that information you could then do something with that data such as update a CRM record, for example.

Webhooks themselves are an HTTP post notifications.  To the non-technie, the notification itself just looks like a bunch of code.  But buried in the code is all the details about the event.  In the case of the RESPONSE_CHANGED webhook for example, the POST includes all the answers the respondent gave to the questions in the survey.

When setting up a webhook, you provide a URL where the information will be sent when the event happens – this is called the “endpoint” or the “listener”.   But at that endpoint, there needs to be some code that “consumes” the information SurveyTown sends and does something with it.   We thought it would be fun to give an example code that takes our RESPONSE CHANGED webhook and makes a CSV out of it responses.  Why?  Perhaps this CSV could then be downloaded into other business intelligence software for example.  But turning the information into a CSV is just one example you could do.  You could do a myriad of different things once you have the data.

So here is the example code, written in PHP that when notified by our RESPONSE_CHANGED webhook creates a CSV.  It should be noted that you could modify this script to change the response data into any format you need –  like XML or JSON. You could even skip saving to a file altogether and pass the data directly into a database or another process. But without further ado, here’s the code:

Example Webhook Endpoint Written in PHP that uses the “RESPONSE_CHANGED” webhook to create a CSV

Happy surveying!