我正在尝试显示当天所有Facebook广告系列的列表...
此代码大部分都可以使用,但它只会显示我的24个广告系列...当我超过100个时。
我不确定为什么会这样......也不确定如何显示当天的统计数据...感谢您的帮助
<?php
$app_id='ID';
$app_secret='SECRET';
$access_token = 'TOKEN';
define('SDK_DIR', __DIR__ . '/..'); // Path to the SDK directory
$loader = include SDK_DIR.'/vendor/autoload.php';
use FacebookAds\Api;
// 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();
use FacebookAds\Object\AdUser;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
use FacebookAds\Object\AdCampaign;
use FacebookAds\Object\Fields\AdCampaignFields;
use FacebookAds\Object\Fields\AdSetFields;
$me = new AdUser('me');
$accounts = $me->getAdAccounts();
// Output account ID
foreach ($accounts as $account) {
echo $account_id = $account->id;
echo "----";
}
$account = new AdAccount($account_id);
$fields = array(
AdCampaignFields::NAME,
AdCampaignFields::OBJECTIVE,
AdCampaignFields::STATUS,
);
$campaigns = $account->getAdCampaigns($fields);
?>
<table border="1" bordercolor="#6699CC" style="width:100%">
<tr>
<th bgcolor="#336699" style="color:#FFF">Campaign_id</th>
<th bgcolor="#336699" style="color:#FFF">Campaign Name</th>
<th bgcolor="#336699" style="color:#FFF">Status</th>
<th bgcolor="#336699" style="color:#FFF">Impressions</th>
<th bgcolor="#336699" style="color:#FFF">Clicks</th>
<th bgcolor="#336699" style="color:#FFF">Spent</th>
</tr>
<?PHP
foreach ($campaigns as $campaign) {
//---------------------------------
?>
<tr>
<td><?php echo $campaign->id; ?></td>
<td><?php echo $campaign->name; ?></td>
<td><?php echo $campaign->campaign_group_status ?></td>
<?php
$fields = array (
'impressions',
'clicks',
'spent',
);
$params = array (
'date_preset'=>'yesterday',
'data_columns'=>"['adgroup_id','actions','spend']",
);
$campaign = new AdCampaign($campaign->id);
$stats = $campaign->getStats($fields, $params);
foreach ($stats as $stat) {
echo '<td>'.$stat->impressions. '</td>';
echo '<td>'.$stat->clicks. '</td>';
echo '<td>'.$stat->spent. '</td>';
}
//---------------------------------
}
?>
答案 0 :(得分:0)
您需要创建一个传入的参数数组。在下面的示例中,我将限制设置为500.
$params = array(
'time_interval' => array(
'day_start' => array('year' =>'2015', 'month'=> '5', 'day'=>'1'),
'day_stop' => array( 'year'=>'2015', 'month'=>'5', 'day'=>'31')
),
'limit' => 500,
);
$fields = array(
AdCampaignFields::NAME,
AdCampaignFields::OBJECTIVE,
AdCampaignFields::STATUS,
);
$campaigns = $account->getAdCampaigns($fields,$params);
答案 1 :(得分:0)
我现在有同样的问题,所以我写这个是对别人的帮助。
您需要明确说明您想循环遍历整个结果集(所有结果页面),并通过将ImplicitFetch
设置为true
来实现。代码示例:
$campaigns = $account->getAdCampaigns($fields);
...
$campaigns->setUseImplicitFetch(true); // set this before loop
...
foreach ($campaigns as $campaign) {
祝你好运, 尼古拉