如何使用facebook php SDK进行批量请求 [英] How to do Batch request using the facebook php SDK

查看:128
本文介绍了如何使用facebook php SDK进行批量请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个表中有很多事件ID。
我想循环这些id,并向Facebook请求该特定ID的附加信息。



现在我为每个不同的http请求ID。我想把它放在一个批次中,所以我可以传递所有的id,并在一个http请求中收到所有这些事件的所有附加信息。



我使用Facebook PHP sdk

解决方案

以批处理两个请求的粗略示例...

  $ batch = array(); 

$ req = array(
'method'=>'GET',
'relative_url'=>'/ me'
);

$ batch [] = json_encode($ req);

$ req = array(
'method'=>'GET',
'relative_url'=>'/ me / albums'
);

$ batch [] = json_encode($ req);

$ params = array(
'batch'=>'['。implode(',',$ batch)']'
);
尝试{
$ info = $ facebook-> api('/','POST',$ params);
} catch(FacebookApiException $ e){
error_log($ e);
$ info = null;
}
if(!empty($ info)){
if($ info [0] ['code'] =='200'){
$ user_profile = json_decode ($信息[0] [ '主体']);
}
if($ info [1] ['code'] =='200'){
$ user_albums = json_decode($ info [1] ['body']);
}
echo< pre> User Profile:\\\
;
print_r($ user_profile);
echo\\\
Albums\\\
;
print_r($ user_albums);
echo< pre>;
}

这是一个粗略的例子,但它应该提供做批处理的基础请求...


In my project i have a lot of event id's in one table. I want to loop over these id's and make a request to facebook for the additional information of that specific id.

Now i make a different http request for each ID. I would like to put this in a batch so i can pass all the id's and receive all the additional information for all of these events in one http request.

I use the Facebook PHP sdk

解决方案

Here is a crude example of doing two requests in a batch ...

$batch = array();

$req = array(
    'method'       => 'GET',
    'relative_url' => '/me'
);

$batch[] = json_encode($req);

$req = array(
    'method'       => 'GET',
    'relative_url' => '/me/albums'
);

$batch[] = json_encode($req);

$params = array(
    'batch' => '[' . implode(',',$batch) . ']'
);
try {
    $info = $facebook->api('/','POST',$params);
} catch(FacebookApiException $e) {
    error_log($e);
    $info = null;
}
if(!empty($info)){
    if($info[0]['code'] == '200'){
        $user_profile = json_decode($info[0]['body']);
    }
    if($info[1]['code'] == '200'){
        $user_albums  = json_decode($info[1]['body']);
    }
    echo "<pre>User Profile:\n";
    print_r($user_profile);
    echo "\nAlbums\n";
    print_r($user_albums);
    echo "<pre>";
}

This is a rough example, but it should provide the basics of doing a batch request ...

这篇关于如何使用facebook php SDK进行批量请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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