在使用 UNIX 时间戳根据
time_created
字段过滤潜在客户时,我遇到了 Facebook Graph API 的问题。看来API的时间过滤并没有精确到秒。
例如,如果我筛选在
06-07-2024 11:58:00 AM
(实际上采用 UNIX 时间戳)创建的潜在客户,并且潜在客户的 time_created 值为 06-07-2024 11:58:12 AM
,则该潜在客户仍包含在响应中,从而导致我的数据中出现重复项。
还有其他人遇到过这个问题或找到一种方法来确保过滤精确到秒吗?
我的代码:
$currentDateTime = Carbon::now(); //At Very Start of Code.
$lastestDate = Carbon::parse(LeadData::latest()->first()->last_processed_time)->getTimestamp();
.
.
.
.
$newUrl2 = "https://graph.facebook.com/v20.0/{$adId}/leads";
$paramsNew2 = [
//'since' => $lastestDate,
'filtering' => json_encode([
[
'field' => 'time_created',
'operator' => 'GREATER_THAN', //_OR_EQUAL
'value' => $latestDate, // 1720247400, getting it from DB as last_processed_time.
]
]),
'fields' => "id,form_id,platform,partner_name,field_data,ad_id,adset_id,campaign_id,ad_name,adset_name,campaign_name,created_time,post",
'access_token' => $accessToken,
];
$response2 = Http::get($newUrl2, $paramsNew2);
$data3 = $response2->json();
$leadDatas = $data3['data'] ?? null;
foreach ($leadDatas as $leadData)
{
try
{
LeadData::create([
'account_id' => $adAccountId ?? null,
'account_name' => null, ///$accountName,
'campaign_id' => $leadData['campaign_id'],
'campaign_name' => $leadData['campaign_name'],
'adset_id' => $leadData['adset_id'],
'adset_name' => $leadData['adset_name'],
'ad_id' => $leadData['ad_id'],
'ad_name' => $leadData['ad_name'],
'ad_status' => $adStatus,
'lead_id' => $leadData['id'],
'lead_created_time' => $leadData['created_time'],
'platform' => $leadData['platform'],
'form_id' => $leadData['form_id'],
'field_data' => json_encode($leadData['field_data'], JSON_UNESCAPED_UNICODE),
'last_processed_time' => $currentDateTime,
]);
} catch (\Exception $e) {
echo "Data entry in database failed: " . $e->getMessage() ."\n";
Log::error($e->getMessage());
}
}
然后我将线索保存在数据库中。
经过一番研究,我得到了答案,显然你可以使用 2 个参数来比较 Graphs API 中的数据,
'filtering' => json_encode([
[
'field' => 'time_created',
'operator' => 'GREATER_THAN',
'value' => $lastestDate, // * 1000) + 1000 // Replace with your actual timestamp value
],
[
'field' => 'time_created',
'operator' => 'LESS_THAN',
'value' => $currentDateTime
]
]),
我的问题实际上并不是以秒为单位进行比较,而是在运行调度程序以输入 $currentDateTimein DB 之后。