After some development work on our end, we have successfully gotten the real-time Facebook lead ad updates to our endpoint using the PHP method the Facebook Dev Team goes over in this video walk through here. However the video is a little grainy at times so it can be hard to see the code, which we added below.
This walk through assumes some basic coding knowledge in PHP as well as the fact you know how to run the PHP files on a publicly accessible server so your callback will work. To make this integration with Facebook and your listener happen you’ll need three things.
1 – Correct Facebook App set up. I will assume you have a Facebook Developer account and know how to set up a basic app. Just make sure on webhook settings that you have a page subscription with the correct callback URL (the public URL you will place the PHP listener, which we will call webhook.php), and the fields are for ‘leadgen’ in the webhook settings. We had issues with the app accessing the listener unless it was on an https domain (so make sure you have a valid SSL cert), and you will also want to give the app domain permissions in any protective service like Cloud Flare or other firewall so Facebook’s payload won’t be blocked from hitting the listener.
2 – PHP listener at your callback URL. We can label this ‘leadhook.php’ or ‘webhook.php’ and call it from the Facebook app settings above. Here’s how we have ours set up for PHP:
<?php if(!session_id()) { session_start(); } require_once('vendor/autoload.php'); use FacebookAds\Object\Lead; $challenge = isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : ""; $verify_token = isset($_REQUEST['hub_verify_token']) ? $_REQUEST['hub_verify_token'] : ""; if ($verify_token == '<your_verification_token>') { echo $challenge; } $input = json_decode(file_get_contents('php://input'), true); error_log(print_r($input, true)); // Output payload into php error logs for additional reference // Write payload contents into a text file for reference $file = fopen("testleads.txt","w") or die("Unable to open file!!!"); echo fwrite($file, json_encode($input)); fclose($file); ?>
3 – Some kind of platform page in your CRM to subscribe the pages. This is what we used below, it’s very basic but works as a good start point for a fuller app integration to your CRM:
Lead Gen Platform
<script> awindow.fbAsyncInit = function() { FB.init({ appId : '<YOUR_APP_ID>', xfbml : true, version : 'v2.5' }); }; (function(d, s, id){ var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) {return;} js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en_US/sdk.js"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk')); function subscribeApp(page_id, page_access_token) { console.log('Subscribed Page to FB Leads Live Update ' + page_id); FB.api('/' + page_id + '/subscribed_apps', 'post', {access_token: page_access_token}, function(response) {console.log('Successfully subscribed page', response); }); } function checkLoginState() { FB.getLoginStatus(function(response) { console.log('statusChangeCallback'); console.log(response); console.log('successfully logged in', response); }); FB.login(function(response) { if (response.status == 'connected') { // Logged into your app and Facebook. FB.api ('me/accounts', function(response) { console.log('successfully retrieved pages', response); var pages = response.data; var select = document.getElementById('pages_list'); for (i = 0, len = pages.length; i < len; i++) { var page = pages[i]; var option = document.createElement('option'); option.value = page.id+";"+page.name; option.innerHTML = page.name; select.appendChild(option); } $("#pages-display").css("display","block"); $("#div_subscribed_pages").css("display", "block"); $("#div-unsubscribe").css("display", "block"); }); } else if (response.status == 'not_authorized') { // The person is logged into Facebook, but not your app. $("#pages-display").css("display","none"); $("#div_subscribed_pages").css("display", "none"); $("#div-unsubscribe").css("display", "none"); } else { // The person is not logged into Facebook, so we're not sure if // they are logged into this app or not. $("#pages-display").css("display","none"); $("#div_subscribed_pages").css("display", "none"); $("#div-unsubscribe").css("display", "none"); } }, {scope: 'public_profile,manage_pages'}); } </script>
Using the above we have successfully had our Facebook lead ads begin to hit our listener at the callback URL and thus allow us to handle the data in real-time from there. The above integration only sets it up to show the raw array from the lead event to hit your listener and get recorded to your error log so you can view it.
Use tail -f in terminal on your server to watch the event come in ‘live’ on the logs. Once you confirm it does, you can then use the $input variable to access the leadgen_id and from there use a GET request to Facebook via their API to pull the actual lead data the user filled out in the form. That part can wait for the next post though!