1

我正在尝试使用 Leadgen 事件实时检索数据我正在使用 Facebook 广告 SDK for php 我成功设置了一个端点来摄取实时 ping 这里是我的 error_log 文件

(
[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

)

我正在尝试检索这种数据

    {
  "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"
    }
  }
}

这是我的 webhook 代码

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

这是错误:

致命错误:未捕获的异常 'Exception' 和消息 'field 'id' 是必需的。在 /www/wibonus/public_html/facebook-php-ads-sdk-master/src/FacebookAds/Object/AbstractCrudObject.php:123 堆栈跟踪:#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} 在第 123 行的 /www/wibonus/public_html/facebook-php-ads-sdk-master/src/FacebookAds/Object/AbstractCrudObject.php 中抛出

4

1 回答 1

2

您可以从 PHP 代码中执行简单的 CURL 请求来检索潜在客户表单数据,而不是使用 Facebook SDK。

这是代码。

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

您应该在回调 URL 中返回 $challenge。(注意:如果您正在使用 webhook)

于 2017-04-18T13:03:44.023 回答