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

<?php
// Verify the webhook origin by checking for the Webhook Key value you defined in SurveyTown
if( empty( $_REQUEST['key' ]) || $_REQUEST['key'] != "amazinglysecurekey" ){
 http_response_code(200);
 die();
}

// Look for the response_changed webhook event. Make sure the response is complete before processing.
if( $_REQUEST['type'] == "response_changed" && ! empty( $_REQUEST['response'] ) && $_REQUEST['response']['isComplete'] == 1 ){ 
 
 $columns = false;
 
 // Use a different CSV file for each survey
 $filename = "survey_responses_{$_REQUEST['survey_id']}.csv";
 
 // Create the CSV header if the file doesn't exist
 if( ! file_exists( $filename ) ){
 $columns = array( 
 "ID", 
 "IP Address", 
 "Start Time",
 "End Time"
   );
 }

// Add response meta data
 $response = array(
 $_REQUEST['response']['responseId'], 
 $_REQUEST['response']['ipAddress'], 
 $_REQUEST['response']['responseStart'], 
 $_REQUEST['response']['responseComplete']
 );
 
 // Loop through all the response data
 foreach( $_REQUEST['response'] as $key => $res ){
 // Questions will have integer keys
 if( is_int( $key ) ){
 // Handle multi value questions
 if( isset( $res['responses'] ) ){
 foreach( $res['responses'] as $choice => $value ){
 if( $columns ){
 $columns[] = strip_tags( $res['questionText'] ) . " ({$value['choiceText']})";
 }
 $response[] = $value['responseValue'];
 }
 // Handle single value questions
 }elseif( isset( $res['response'] ) ){
 if( $columns ){
 $columns[] = strip_tags( $res['questionText'] );
 }
 $response[] = $res['response']['responseValue'];
 }
 }
 }

// Open and write to the CSV
 $fp = fopen( $filename, "a" );
 if( $columns ){
 fputcsv( $fp, $columns );
 }
 fputcsv( $fp, $response );
 fclose( $fp ); 
}

http_response_code(200);

Happy surveying!