Facebook开发者 - 广告管理 - 主要广告 - 阅读线索 - 批量阅读

时间:2016-11-30 17:51:08

标签: php facebook facebook-graph-api lead

我试图使用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));
?>

这是错误:

  

致命错误:未捕获的异常&#39;异常&#39;消息&#39;字段&#39; id&#39;是必需的。&#39;在/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-&gt; assureId()#1 /www/wibonus/public_html/webhook.php(38):FacebookAds \ Object \ Ad-&gt; getLeads ()在第123行的/www/wibonus/public_html/facebook-php-ads-sdk-master/src/FacebookAds/Object/AbstractCrudObject.php中抛出#2 {main}

1 个答案:

答案 0 :(得分:1)

您可以从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);

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