将响应与Facebook批处理请求相关联 [英] Linking responses to requests with Facebook Batch Requests

查看:119
本文介绍了将响应与Facebook批处理请求相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Facebook的批次请求发布到多个Feed我需要将正确的响应链接到批处理中的每个请求。既然我发现文档中没有确定的信息,那么返回的数组的成员是否按照请求的顺序显示出来?



换句话说,如果我收到错误在返回的数组的第三个成员中,这是否正确表示错误是指在批次中发送的第三个请求?



我可以使用id成功请求,但是错误消息看起来是一般的,并且不会将任何数据链接到生成它们的请求(除非我缺少某些东西)。

解决方案

是的,这是正确的。



我的策略是在加载批处理请求时创建跟踪数组。该数组将我的关联数组的键与我发布批次的数字顺序相关联。当我循环结果时,我使用一个计数器来遍历跟踪数组,并拉出适当的关联数组索引。然后我使用它来更新关联数组与批处理操作的步骤的结果。



如果批处理支持'name'参数和该参数,这将是很好的每次回复都返回。但是,如果您使用该名称来创建批处理依赖关系,则只能执行此操作:
https://developers.facebook.com/docs/reference/api/batch/



加载批次:

  foreach($ campaign as $ title => $ campaign){
if(count($ batch)== 20){
$ batchches [] = $ batch;
$ batch = array();
}

$ titles [] = $ title; #TRACKING数组;
$ body = http_build_query($ campaign);
$ body = urldecode($ body);

$ batch [] = array(
'method'=>'POST',
'relative_url'=>/ act _ {$ act} / adcampaigns
'body'=> $ body
);
}

处理批次:



($ batch){
$ batchches [] = $ batch;
$ counter = 0;

foreach($ batchches as $ batch){
$ params = array(
'access_token'=> $ access_token,
'batch'=> json_encode ($ batch)
);

$ responses = $ facebook-> api('/','POST',$ params);

foreach($ respond as $ response){
$ response = json_decode($ response ['body'],1);
$ campaign_id = $ response ['id'];
$ title = $ titles [$ counter];从追踪阵列中获取索引
$ campaigns [$ title] ['campaign_id'] = $ campaign_id;
$ counter ++; #建立计数器
}
}
}


I'm using Facebook's Batch Requests to post to multiple feeds and I need to link the correct response to every request in the batch. Since I found no definitive info on the documentation, do the members of the returned array appear in the same order as the requests?

In other words, if I get an error in the third member of the returned array, does that positively mean that the error refers to the third request I sent in the batch?

I can use the id for succesful requests, but error messages seem general and do not bring any data linked to the request that generated them (unless I'm missing something).

解决方案

Yes, that's correct.

My strategy is that I create a tracking array as I load up my batch requests. This array correlates the key for my associative array to the numerical order I posted the batches. When I loop over the results, I use a counter to step through the tracking array and pull out the proper associative array index. Then I use that to update the associative array with the results from that step of the batch operation.

It would be nice if batching supported the 'name' parameter and that parameter got returned with each response. But that only appears to work if you're using the name to create batch dependencies: https://developers.facebook.com/docs/reference/api/batch/

Loading up the batches:

foreach ($campaigns as $title => $campaign) {
    if (count($batch) == 20) {
        $batches[] = $batch;
        $batch = array();
    }

    $titles[] = $title;  #TRACKING array;
    $body = http_build_query($campaign);
    $body = urldecode($body);

    $batch[] = array(
        'method' => 'POST',
        'relative_url' => "/act_{$act}/adcampaigns",
        'body' => $body
    );
}

Processing the batches:

if ($batch) {
    $batches[] = $batch;
    $counter = 0;

    foreach ($batches as $batch) {
        $params = array(
          'access_token' => $access_token,
          'batch' => json_encode($batch)
        );

        $responses = $facebook->api('/', 'POST', $params);

        foreach ($responses as $response) {
            $response = json_decode($response['body'], 1);
            $campaign_id = $response['id'];
            $title = $titles[$counter];  #RETRIEVING THE INDEX FROM THE TRACKING ARRAY
            $campaigns[$title]['campaign_id'] = $campaign_id;
            $counter++; #INCREMENTING THE COUNTER
        }
    }
}

这篇关于将响应与Facebook批处理请求相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆