Facebook API:无法检索Facebook主导广告 [英] Facebook API: Can't retrieve Facebook Lead Ads

查看:287
本文介绍了Facebook API:无法检索Facebook主导广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功设置了一个webhook来指向我的回调URL,它收到以下示例数据:

I have set up successfully a webhook to point to my callback URL and it receives the following example data:

Array
(
[object] => page
[entry] => Array
    (
        [0] => Array
            (
                [id] => 297269987142139
                [time] => 1454876106
                [changes] => Array
                    (
                        [0] => Array
                            (
                                [field] => leadgen
                                [value] => Array
                                    (
                                        [ad_id] => 410908499427
                                        [adgroup_id] => 238113717077
                                        [created_time] => 1454847306
                                        [form_id] => 577581087825
                                        [leadgen_id] => 441393665441
                                        [page_id] => 297269987142139
                                    )
                            )
                    )
            )
    )
 )

但是,如果我尝试检索与 leadgen_id 相关联的数据为由facebook解释,它不起作用:

But if I try to retrieve the data associated with the leadgen_id as explained by facebook, it doesn't work:

 use FacebookAds\Object\Lead;

 $form = new Lead('441393665441');
 $form->read();

我收到以下错误:不支持的获取请求。请阅读Graph API文档,网址为:https://developers.facebook.com/docs/graph-api

不知道为什么它不起作用。

I've tried everything but can't figure out why it doesn't work.

我正在发送这样的测试线索:

I am sending test leads like this:

    curl \
    -F "object_id=<PAGE_ID>" \
    -F "object=page" \
    -F "field=leadgen" \
    -F "access_token=<ACCESS_TOKEN>" \
    -F 'custom_fields={"page_id": <PAGE_ID>}' \
    "https://graph.facebook.com/<API_VERSION>/<APP_ID>/subscriptions_sample"


推荐答案

我也努力让这个工作,似乎没有人做出任何复制和粘贴解决方案似乎有效。

I also struggled to get this working, and it seems that nobody has made any copy-and-paste solution that seemed to work.

这个PHP函数适用于我:

This PHP function works for me:

function getLead($leadgen_id,$user_access_token) {
    //fetch lead info from FB API
    $graph_url= 'https://graph.facebook.com/v2.5/'.$leadgen_id."?access_token=".$user_access_token;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $graph_url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $output = curl_exec($ch); 
    curl_close($ch);

    //work with the lead data
    $leaddata = json_decode($output);
    $lead = [];
    for($i=0;$i<count($leaddata->field_data);$i++) {
        $lead[$leaddata->field_data[$i]->name]=$leaddata->field_data[$i]->values[0];
    }
    return $lead;
}

您可以使用此示例代码的功能:

You can use the function with this sample code:

//Take input from Facebook webhook request
$input = json_decode(file_get_contents('php://input'),true);
$leadgen_id = $input["entry"][0]["changes"][0]["value"]["leadgen_id"];

//Token - you must generate this in the FB API Explorer - tip: exchange it to a long-lived (valid 60 days) token
$user_access_token = 'TOKENID';

//Get the lead info
$lead = getLead($leadgen_id,$user_access_token);//get lead info

从这里你可以通过获取属性和值来发送实时邮件:

From here on you can for example send a realtime mail by getting attributes and values in the lead:

//Create an email with the lead info
$mail="<html><body><h2>New lead</h2><blockquote>";
$mail.="Lead id: ".$leadgen_id."<br>";
foreach($lead as $attr=>$val) {
    $mail.=$attr.": ".$val."<br>";
}
$mail.="</blockquote></body></html>";

我希望这有帮助。请注意,subscriptions_sample lead不包含任何字段。您可以使用自己的Facebook个人资料来测试以获取真实数据。

I hope this helps. Notice that the subscriptions_sample lead doesn't contain any fields. You can test with your own Facebook profile to get real data.

这篇关于Facebook API:无法检索Facebook主导广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆