i'm trying to retrieve data in real time using the leadgen event i'm using the Facebook ads SDK for php i succeeded in seting up an endpoint to ingest the realtime ping here is my error_log file
(
[entry] => Array
(
[0] => Array
(
[changes] => Array
(
[0] => Array
(
[field] => leadgen
[value] => Array
(
[ad_id] => 0
[form_id] => 1602570684541170
[leadgen_id] => 1629665456004730
[created_time] => 148036545636
[page_id] => 1870793756647618
[adgroup_id] => 0
)
)
)
[id] => 1870793717274654
[time] => 14803464737
)
)
[object] => page
)
i'm trying to retrieve this kind of data
{
"data": [
{
"created_time": "2015-02-28T08:49:14+0000",
"id": "<LEAD_ID>",
"ad_id": "<AD_ID>",
"form_id": "<FORM_ID>",
"field_data": [
{
"name": "car_make",
"values": [
"Honda"
]
},
{
"name": "full_name",
"values": [
"Joe Example"
]
},
{
"name": "email",
"values": [
"joe@example.com"
]
},
]
}
],
"paging": {
"cursors": {
"before": "OTc2Nz3M5MTgyMzU1NDMy",
"after": "OTcxNjcyOTg5ANTI4NzE4"
}
}
}
here is my webhook code
<?php
require_once 'facebook-php-ads-sdk-master/vendor/autoload.php';
use FacebookAds\Object\LeadgenForm;
use FacebookAds\Api;
use FacebookAds\Object\Ad;
use FacebookAds\Object\Lead;
$app_id='148247344564502719';
$app_secret='17489f3e0f8235645413f5dd593e67787';
$access_token='1482473465452719';
// Initialize a new Session and instanciate an Api object
Api::init($app_id, $app_secret, $access_token);
// The Api object is now available trough singleton
$api = Api::instance();
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'abc123') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
error_log(print_r($input, true));
$form_id = $input['entry'][0]['changes'][0]['value']['form_id'];
$ad_id = $input['entry'][0]['changes'][0]['value']['ad_id'];
$leadgen_id = $input['entry'][0]['changes'][0]['value']['leadgen_id'];
/*$form = new Lead($leadgen_id);
$form->read();
$ad = new Ad($ad_id);
$leads = $ad->getLeads();*/
$form = new LeadgenForm($form_id);
$leads = $form->getLeads();
error_log(print_r($leads, true));
?>
here is the error :
Fatal error: Uncaught exception 'Exception' with message 'field 'id' is required.' in /www/wibonus/public_html/facebook-php-ads-sdk-master/src/FacebookAds/Object/AbstractCrudObject.php:123 Stack trace: #0 /www/wibonus/public_html/facebook-php-ads-sdk-master/src/FacebookAds/Object/Ad.php(290): FacebookAds\\Object\\AbstractCrudObject->assureId() #1 /www/wibonus/public_html/webhook.php(38): FacebookAds\\Object\\Ad->getLeads() #2 {main} thrown in /www/wibonus/public_html/facebook-php-ads-sdk-master/src/FacebookAds/Object/AbstractCrudObject.php on line 123
Instead of using Facebook SDK you can do simple CURL request from your PHP code to retrieve lead form data.
Here is the code.
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'abc123') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
error_log(print_r($input, true));
$form_id = $input['entry'][0]['changes'][0]['value']['form_id'];
$ad_id = $input['entry'][0]['changes'][0]['value']['ad_id'];
$leadgen_id = $input['entry'][0]['changes'][0]['value']['leadgen_id'];
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'abc123') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
error_log(print_r($input, true));
$form_id = $input['entry'][0]['changes'][0]['value']['form_id'];
$ad_id = $input['entry'][0]['changes'][0]['value']['ad_id'];
$leadgen_id = $input['entry'][0]['changes'][0]['value']['leadgen_id'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v2.8/" . $leadGenId . "?access_token={ACCESS_TOKEN}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$leadData = json_decode($result, true);
$lead = [];
for($i = 0; $i < count($leadData); $i++)
{
$lead[$leadData[$i]['name']] = $leadData[$i]['values'][0];
}
print_r($lead);
You should return $challenge in your callback URL. (Note:If you are working on webhook)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.