簡體   English   中英

廣告系列見解返回空的Facebook Marketing API

[英]Campaign insights return null Facebook Marketing API

我正在抓取與用戶相關聯的所有廣告系列,並試圖了解這些廣告系列。 我得到的廣告系列效果很好,但是當我嘗試獲取見解時,它總是返回null。 我試圖改變日子和不同領域。

$api = Api::instance();

$id = trim($_POST['actid']);

$account = new AdAccount($id);

$fields = array(
    CampaignFields::NAME,
    CampaignFields::ID,
);

$campaigns = $account->getCampaigns($fields);



$cleanArray = [];
foreach ($campaigns as $campaign) {

    $campFields = array(
    AdsInsightsFields::CPC,
);

$params = array(
    'time_range' => array(
    'since' => (new DateTime("-1 day"))->format('Y-m-d'),
    'until' => (new DateTime('NOW'))->format('Y-m-d'),
    ),
);

$campInsights = $campSelect->getInsights($campFields, $params);
$innerArray = [];
array_push($innerArray,$campaign->{CampaignFields::NAME});
array_push($innerArray,$campaign->{CampaignFields::ID});
array_push($innerArray,$campInsights->{AdsInsightsFields::CPC});
array_push($cleanArray,$innerArray);
}

header('Content-Type: application/json');
echo json_encode($cleanArray);

調用getInsights返回一個迭代器,因為收集見解的請求可以返回一組結果。

如果確定只返回單個結果,則可以訪問$campInsights[0]

您在此行的代碼中也有錯誤,因為未定義$campSelect

$campInsights = $campSelect->getInsights($campFields, $params);

它應該是:

$campInsights = $campSelect->getInsights($campFields, $params);

所以這是一個范圍問題,因為我仍然需要遍歷該對象作為數組。 發布此信息以幫助他人。

foreach($campInsights as $insights){
    $cpc = $insights->{AdsInsightsFields::CPC}.PHP_EOL;
    $clicks = $insights->{AdsInsightsFields::WEBSITE_CLICKS}.PHP_EOL;
}

遍歷對象后,便可以獲取值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM