Facebook API: Can't retrieve Facebook Lead Ads

泄露秘密 提交于 2019-11-30 21:58:41

问题


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

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

I get the following error: Unsupported get request. Please read the Graph API documentation at 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"

回答1:


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

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>";

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.




回答2:


Thanks @John Sandy It helped a lot:-)

However if someone else is also struggling please use below url in browser itself.

https://graph.facebook.com/v2.5/leadgen_id?access_token=xxxxxxxx

Note: insert your leadgen_id and access token

Hope this will help

thanks Pranav




回答3:


I have done this using JavascriptSDK and ReactJS. You just need page access token to retrive the values from the lead. Make sure you already subscibed the page with your app. You need to make a GET call on:

https://graph.facebook.com/v2.11/<leadgen_id>?access_token=<page_access_token>

For more info kindly check the link: http://tanmayverma.com/reactjs/storing-facebook-lead-data-in-our-database-in-real-time-using-reactjs-meteorjs-and-fbs-javascript-sdk/



来源:https://stackoverflow.com/questions/35259024/facebook-api-cant-retrieve-facebook-lead-ads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!