在单个API调用中加载Facebook adinterests列表

时间:2015-04-13 09:38:22

标签: facebook facebook-ads-api geotargetting

有没有办法在单个API调用中按ID加载多个adinterests?

这是情况: 我们针对广告定位广告系列提供了多个adinterests。例如,我们的目标用户是:

  • 音乐(ID = 111)
  • arts(ID = 222)
  • 博物馆(ID = 333)

我们列出了用户的所有兴趣,但数据库中的我们仅存储了ID

目前我们只能为每个请求加载单个利息,例如:

https://graph.facebook.com/v2.3/111?access_token=___
https://graph.facebook.com/v2.3/222?access_token=___
https://graph.facebook.com/v2.3/333?access_token=___

https://graph.facebook.com/v2.3/?access_token=___&type=adinterest&id=111

这很有效。但是,我想在一次调用中加载所有兴趣以加快页面加载,例如:

https://graph.facebook.com/v2.3/?type=adinterest&id[0]=111&id[1]=222&id[2]=333&access_token=___

不幸的是,这不起作用。 那么,有没有办法按提供的ID列表加载广告兴趣列表?

2 个答案:

答案 0 :(得分:3)

您可以使用批处理请求框架向多个端点发出单个HTTP请求。看到 https://developers.facebook.com/docs/marketing-api/batch-requests

答案 1 :(得分:-2)

以下是使用流的PHP解决方案:

$params = [
    'access_token' => '________',
    'batch' => json_encode([
        ['relative_url' => 'v2.3/6002969077368', 'method' => 'GET'],
        ['relative_url' => 'v2.3/6003398522803', 'method' => 'GET'],
    ])
];

$opts = array(
    'http'=>array(
        'method'=>"POST",
    )
);
$context = stream_context_create($opts);

$url = 'https://graph.facebook.com/?'.http_build_query($params);
$resp = file_get_contents($url, false, $context);

$resp = json_decode($resp, true);
foreach($resp as $item){
    echo '<pre>'.print_r(json_decode($item['body'], true), true).'</pre>';
}

将输出:

Array
(
    [id] => 6002969077368
    [name] => British Museum
    [audience_size] => 1593400
    [path] => Array
        (
        )
    [description] =>
)

Array
(
    [id] => 6003398522803
    [name] => Comic book
    [audience_size] => 113805160
    [path] => Array
        (
        )
    [description] =>
)

请注意,Facebook批量请求必须是POST类型,而读取ID必须是GET类型。

同时指定 API版本 relative_url (v2.3)不是必需的,但FB将使用API​​版本,具体取决于您使用access_token的Facebook APP。这意味着如果您很久以前创建了FB应用程序,它可能会自动切换到较旧的API版本(例如v2.2),但如果您部署在使用新创建的FB App的较新服务器上,它可能会切换到较新的API(例如v2.3)并抛出意外错误(例如不推荐使用的属性)。因此,为每个URL请求明确指定FB API版本是一个好习惯。