简体   繁体   English

FB Ads API (#17) 达到用户请求限制

[英]FB Ads API (#17) User request limit reached

I am working on Facebook ads api to get the account Campaign data.What I am doing here is I get list of all campaigns and doing forloop of each campaign get Campaign stat我正在使用 Facebook 广告 api 来获取帐户活动数据。我在这里做的是获取所有活动的列表,并对每个活动进行 forloop 获取活动统计信息

$campaignSets = $account->getCampaigns(array(
      CampaignFields::ID,
      CampaignFields::NAME
));

foreach ($campaignSets as $campaign) {
      $campaign = new Campaign($campaign->id);
      $fields = array(
        InsightsFields::CAMPAIGN_NAME,
        InsightsFields::IMPRESSIONS,
        InsightsFields::UNIQUE_CLICKS,
        InsightsFields::REACH,
        InsightsFields::SPEND,
        InsightsFields::TOTAL_ACTIONS,
        InsightsFields::TOTAL_ACTION_VALUE
      );
      $params = array(
        'date_preset' => InsightsPresets::TODAY
      );
                $insights = $campaign->getInsights($fields, $params);
}

when executing above code I am getting error as (#17) User request limit reached.执行上面的代码时,我收到错误,因为(#17)达到了用户请求限制。

Can anyone help me how to solve this kind of error?谁能帮我解决这种错误?

Thanks, Ronak Shah谢谢,罗纳克沙

You should consider generating a single report against the adaccount which returns insights for all of your campaigns, this should reduce the number of requests required significantly.您应该考虑针对 Adaccount 生成一份报告,该报告会返回您所有广告系列的见解,这应该会显着减少所需的请求数量。

Cursor::setDefaultUseImplicitFetch(true);

$account = new AdAccount($account_id);
$fields = array(
  InsightsFields::CAMPAIGN_NAME,
  InsightsFields::CAMPAIGN_ID,
  InsightsFields::IMPRESSIONS,
  InsightsFields::UNIQUE_CLICKS,
  InsightsFields::REACH,
  InsightsFields::SPEND,
  InsightsFields::TOTAL_ACTIONS,
  InsightsFields::TOTAL_ACTION_VALUE,
);
$params = array(
  'date_preset' => InsightsPresets::TODAY,
  'level' => 'ad',
  'limit' => 1000,
);

$insights = $account->getInsights($fields, $params);
foreach($insights as $i) {
  echo $i->campaign_id.PHP_EOL;
}

If you run into API limits, your only option is to reduce calls.如果您遇到 API 限制,您唯一的选择就是减少调用。 You can do this easily by delaying API calls.您可以通过延迟 API 调用轻松地做到这一点。 I assume you are already using a Cron Job, so implement a counter that stores the last campaign you have requested the data for.我假设您已经在使用 Cron 作业,因此实现一个计数器来存储您请求数据的最后一个活动。 When the Cron Job runs again, request the data of the next 1-x campaign data (you have to test how many are possible per Cron Job call) and store the last one again.当 Cron Job 再次运行时,请求下一个 1-x 活动数据的数据(您必须测试每个 Cron Job 调用可能有多少)并再次存储最后一个。

Also, you should batch the API calls - it will not avoid limits, but it will be a lot faster.此外,您应该批处理 API 调用 - 它不会避免限制,但会快得多。 As fast as the slowest API call in the batch.与批处理中最慢的 API 调用一样快。

Add this to your code and you'll never have to worry about FB's Rate Limiting/User Limit Reached.将此添加到您的代码中,您将永远不必担心 FB 的速率限制/达到用户限制。 Your script will automatically sleep as soon as you approach the limit, and then pick up from where it left after the cool down.一旦接近限制,您的脚本将自动休眠,然后在冷却后从它离开的地方开始。 Enjoy :)享受 :)

import logging
import requests as rq

#Function to find the string between two strings or characters
def find_between( s, first, last ):
    try:
        start = s.index( first ) + len( first )
        end = s.index( last, start )
        return s[start:end]
    except ValueError:
        return ""

#Function to check how close you are to the FB Rate Limit
def check_limit():
    check=rq.get('https://graph.facebook.com/v3.3/act_'+account_number+'/insights?access_token='+my_access_token)
    call=float(find_between(check.headers['x-business-use-case-usage'],'call_count":','}'))
    cpu=float(find_between(check.headers['x-business-use-case-usage'],'total_cputime":','}'))
    total=float(find_between(check.headers['x-business-use-case-usage'],'total_time":',','))
    usage=max(call,cpu,total)
    return usage

#Check if you reached 75% of the limit, if yes then back-off for 5 minutes (put this chunk in your loop, every 200-500 iterations)
if (check_limit()>75):
    print('75% Rate Limit Reached. Cooling Time 5 Minutes.')
    logging.debug('75% Rate Limit Reached. Cooling Time 5 Minutes.')
    time.sleep(300)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM