在我的项目中,我在一个表中有很多事件ID。 我想循环这些ID并向Facebook发出请求,以获取该特定ID的附加信息。如何使用Facebook的PHP SDK批量请求
现在我为每个ID做不同的http请求。我想把它放在一个批处理中,这样我就可以传递所有的id,并在一个http请求中接收所有这些事件的所有附加信息。
我使用Facebook PHP SDK
在我的项目中,我在一个表中有很多事件ID。 我想循环这些ID并向Facebook发出请求,以获取该特定ID的附加信息。如何使用Facebook的PHP SDK批量请求
现在我为每个ID做不同的http请求。我想把它放在一个批处理中,这样我就可以传递所有的id,并在一个http请求中接收所有这些事件的所有附加信息。
我使用Facebook PHP SDK
可以批量这些请求是这样的:
curl \
-F 'access_token=…' \
-F 'batch=[ \
{"method": "GET", "relative_url": "me"}, \
{"method": "GET", "relative_url": "me/friends?limit=50"} \
]'\
https://graph.facebook.com
https://developers.facebook.com/docs/reference/api/batch/
还有就是50
这里是批量大小的限制在批次中执行两个请求的粗略示例...
$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>";
}
这是一个粗略的例子,但它应该提供做批处理的请求的基础...
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('YOUR_APP_ID','YOUR_APP_SECRET');
// Use one of the helper classes to get a FacebookSession object.
// FacebookRedirectLoginHelper
// FacebookCanvasLoginHelper
// FacebookJavaScriptLoginHelper
// or create a FacebookSession with a valid access token:
$session = new FacebookSession('access-token-here');
try {
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
$params = [
[ // call 0 - get current logged in user
"method" => "GET",
"relative_url" => "me"
],
[ // call 1 - get current logged in user's friends
"method" => "GET",
"relative_url" => "me/friends"
],
[ // call 3 - get current logged in user's likes
"method" => "GET",
"relative_url" => "me/likes"
],
[// call 4 - get current logged in user's albums and photos
"method" => "GET",
"relative_url" => "method/fql.multiquery/?queries=" . json_encode([
"albums" => urlencode("SELECT aid, object_id, type, name, visible, owner, cover_pid, cover_object_id, visible, photo_count, video_count FROM album WHERE owner=me()"),
"album_covers" => urlencode("SELECT src_big, src_small, images, aid FROM photo WHERE pid IN (SELECT cover_pid FROM #albums)"),
"photos" => urlencode("SELECT pid, object_id, owner, src_big, src_small, images, aid FROM photo WHERE aid IN (SELECT aid FROM #albums)")
])
];
$response = (new FacebookRequest($session, 'POST', '?batch='.json_encode($params)))->execute();
$objects = $response->getGraphObject();
foreach($objects->asArray() as $object){
$body = json_decode($object->body, 1);
print_r($body);
echo "--------------\n";
}
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
我已签出的文件,但它不是为PHP SDK。 这个例子是针对2个分开的查询的。我想问这个查询。 $这 - > facebook-> API( \t \t \t \t \t $ ID, 'GET', \t \t \t \t \t阵列( \t \t \t \t \t \t '字段'=>“名称,地点,隐私, location,start_time,end_time,description')); 但是50个Id一次。不知道批处理请求是否可行。 Thx为您提供帮助! –
然后你只需要调用'$ this-> facebook-> api($ id,'GET',array('fields'=>'name,venue,privacy,location,start_time,end_time,description','ids' =>'[用户ID的CSV列表]));' – Igy
Thx!会试试这个! –