[英]Campaign insights return null Facebook Marketing API
I am grabbing all of the campaigns associated with a user and trying to get insights for the campaigns. 我正在抓取与用户相关联的所有广告系列,并试图了解这些广告系列。 I get the campaigns just fine but when I try to get the insights it always returns null. 我得到的广告系列效果很好,但是当我尝试获取见解时,它总是返回null。 I have tried changing the days and different fields. 我试图改变日子和不同领域。
$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);
The call to getInsights
returns an iterator, as requests to gather insights can return a set of results. 调用getInsights
返回一个迭代器,因为收集见解的请求可以返回一组结果。
If you're sure it will only return a single result, you can just access $campInsights[0]
. 如果确定只返回单个结果,则可以访问$campInsights[0]
。
You also have an error in your code in this line, as $campSelect
is not defined: 您在此行的代码中也有错误,因为未定义$campSelect
:
$campInsights = $campSelect->getInsights($campFields, $params);
It should be: 它应该是:
$campInsights = $campSelect->getInsights($campFields, $params);
So it was a scope issue as I still needed to iterate through the object as an array. 所以这是一个范围问题,因为我仍然需要遍历该对象作为数组。 Posting this to help others. 发布此信息以帮助他人。
foreach($campInsights as $insights){
$cpc = $insights->{AdsInsightsFields::CPC}.PHP_EOL;
$clicks = $insights->{AdsInsightsFields::WEBSITE_CLICKS}.PHP_EOL;
}
After I looped through the object, I was able to grab the values. 遍历对象后,便可以获取值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.