检索主要广告Facebook API

时间:2017-05-25 11:36:44

标签: facebook facebook-php-sdk ads

我在检索潜在客户广告时遇到了问题。

我有Ad-ID和Page-ID。我没有创建它们,但是作为开发人员添加了。

我试图使用PHP SDK和https://developers.facebook.com/docs/marketing-api/guides/lead-ads/v2.9

什么都没有用。我找不到一个很好的教程。 我只想找回领先的广告!

任何?

2 个答案:

答案 0 :(得分:5)

假设您已经安装了FB API SDK并配置了您的FB应用程序,您可以使用它来获取$page_id的所有LeadAd的所有结果

use FacebookAds\Api;
use FacebookAds\Object\Page;
use FacebookAds\Object\Ad;

$access_token = 'YOUR TOKEN';
$app_id = 'YOUR APP ID';
$app_secret = 'YOUR APP SECRET';

$page_id = 'YOUR PAGE ID';

Api::init($app_id, $app_secret, $access_token);

$ads = getAllLeadsAds($page_id);

$result = array();

foreach ($ads->data as $item) {

    $leads = getLeadAdInfo($item->id);
    $i = 0;
    foreach ($leads->data as $value) {
        $result[$i]['ad_id'] = $item->id;
        $result[$i]['lead_id'] = $value->id;
        $result[$i]['form'] = $value->field_data;
        $i++;
    }
}

print_r($result);

function getAllLeadsAds($page)
{
    $page = new Page($page);
    return $page->getLeadgenForms()->getResponse()->getBody();
}

function getLeadAdInfo($ad)
{
    $ad = new Ad($ad);
    return $ad->getLeads()->getResponse()->getBody();
}

答案 1 :(得分:0)

您可能没有调用adset中的所有广告。

您可以使用Facebook光标。

$ cursor-> fetchAfter();

use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Values\ArchivableCrudObjectEffectiveStatuses;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\AdCampaign;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdFields;
use FacebookAds\Object\Ad;
use FacebookAds\Object\Campaign;

function getadsetname($adset_id,$acc_id){

  $adset = new AdSet($adset_id, $acc_id);
  $adset->read(array(
    AdSetFields::NAME,
  ));

  return $adset->name; // Outputs name of adset.
}
function get_campaignname($campaign_id){

  $campaign = new Campaign($campaign_id);
  $campaign->read(array(
    CampaignFields::NAME,
  ));
  return $campaign->name;
}

$account = new AdAccount($account_id);

echo $adset_name = getadsetname($adset_id,$account_id);
echo $campaign_name = get_campaignname($campaign_id);
echo "<hr>";

$adcampaign = new AdAccount($campaign_id);  

$adset = new AdSet($adset_id);
$ads = $adset->getAds(array(
    AdFields::NAME,
    AdFields::CONFIGURED_STATUS,
    AdFields::EFFECTIVE_STATUS,
    AdFields::CREATIVE,
   ));

$ads->fetchAfter();

foreach ($ads as $ad) {

    $ad_id = $ad->id;
    $ad_name = $ad->name;

    if($ad->configured_status=="ACTIVE"){

        $ad1 = new Ad($ad_id);
        $leads = $ad1->getLeads();  
        $leads->fetchAfter();

        foreach ($leads as $lead) {

            $fname = $lead->field_data;
            //var_dump($fname);

            $data = array();
            $data['lead_id']        = $lead->id;
            $data['created_time']   = $lead->created_time;
            $data['ad_id']          = $ad_id;
            $data['ad_name']        = $ad_name;
            $data['adset_id']       = $adset_id;
            $data['adset_name']     = $adset_name;//$adset_name;
            $data['campaign_id']    = $campaign_id;
            $data['campaign_name']  = $campaign_name;
            $data['form_id']        = $lead->form_id;//$lead->id;
            $data['is_organic']     = $lead->is_organic;//$lead->id;


        }

    }
}

$ ads-> fetchAfter();将获得下一个广告列表, $ leads-> fetchAfter();将获得下一个线索列表