Ad Code

step-by-step guide to set up Facebook Lead Ads Webhook inside your Facebook App to receive leads on a PHP server

Here’s a step-by-step guide to set up Facebook Lead Ads Webhook inside your Facebook App to receive leads on a PHP server:


1. Create a Facebook App

Go to: https://developers.facebook.com/apps

Click "Create App"

Select "Business" as the type

Set:

App Name

Contact Email

Click Create App 


2. Add Webhooks Product

Inside your App, click “+ Add Product” on the left

Find Webhooks and click "Set Up" 


3. Set Up Webhook for Page Object

Click "Subscribe to this object" under Page


In the form:

Callback URL → your live PHP script URL (e.g. https://yourdomain.com/webhook.php)

Verify Token → use a custom string (e.g. my_verify_token)


Click Verify and Save

⚠️ Your server must:

Use HTTPS

Respond with hub.challenge for verification (handled in webhook.php) 


4. Add Permissions

Under App Review > Permissions and Features, request:

pages_read_engagement

leads_retrieval


pages_manage_metadata

🧪 For development:

Add yourself as a Tester or Admin

Connect a test Facebook Page with a Lead Ad form
 

5. Generate Page Access Token

Go to Graph API Explorer:

Select your App


Get a token with:

pages_read_engagement

leads_retrieval

pages_manage_metadata

Select a Page, then click Generate Token

Save this token securely 


6. Subscribe Page to App (Webhook)

Use the access token and run this cURL command: bash
CopyEdit
curl -X POST "https://graph.facebook.com/v18.0/{PAGE_ID}/subscribed_apps" \ -d "subscribed_fields=leadgen" \ -d "access_token=PAGE_ACCESS_TOKEN"


Replace:

{PAGE_ID} → your Facebook Page ID

PAGE_ACCESS_TOKEN → token you just generated
 

7. Test the Setup

Go to Meta for Developers > Tools > Lead Ads Testing Tool
🔗 https://developers.facebook.com/tools/lead-ads-testing


Choose your Page & Form


Click "Create Lead"


Check:

Your webhook.php gets hit (check fb_leads_log.json)

Log file shows lead data or lead ID
 

Files Recap

PHP File: webhook.php (handles GET verify + POST leads)

Live URL: Must be HTTPS and publicly accessible

FB App Setup: Webhooks → Page → Callback + Verify Token

Page Subscription: Done via Graph API

Permissions: leads_retrieval, etc.





Post a Comment

0 Comments