Inbound Horizons

Small Business Digital Marketing Partners Since 2010

Getting Facebook Lead Ads Real-Time Data To An Endpoint For Integrating Into A CRM


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.

facebook lead ads integration

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:

if(!session_id()) {


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));

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

awindow.fbAsyncInit = function() {
      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); = id;
     js.src = "//";
     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', 
	 {access_token: page_access_token},
	 {console.log('Successfully subscribed page', response);
function checkLoginState() {

 	FB.getLoginStatus(function(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 =;
	    	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 =";";
		        option.innerHTML =;
		  	$("#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.
	    $("#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.
            $("#div_subscribed_pages").css("display", "none");
	    $("#div-unsubscribe").css("display", "none");
	}, {scope: 'public_profile,manage_pages'}); 

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.

facebook lead ads to an endpoint

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!

Leave a Reply

filtering and replacing wordpress loop content
Coding & Dev TipsIBH Company Blog

How To Filter And Replace HTML In The Wordpress Content Loop

Admin Jun 15 2017
FAA Commercial Drone Registrations By State
Business Marketing TipsDrone Video Tips & InfoDrone VideographyIBH Company Blog

Top 10 Commercial Drone Registrations By State & County

Admin May 24 2017
Google digital breakfast in Buda TX
Business Marketing TipsIBH Company Blog

Why Not Get Free Breakfast With Google For National SMB Week?

Admin Apr 27 2017
Conversion OptimizationIBH Company BlogWebsite Optimizations

How To Reduce Bounce Rate Over 90% With One Simple Fix

Admin Mar 17 2017
[contact-form-7 id="273" title="Short Form - Main"]

Please leave this field empty.

[contact-form-7 id="319" title="Rep Form - Main"]
I Am Interested In:

Please leave this field empty.