我在我的网站http://www.cefozyt.com上开发了一个应用程序,它可以向多个 facebook 用户墙和群组发布链接、消息等。我用了 :-
if($user){ // 继续知道您有一个登录用户,该用户具有有效会话。
//========= 使用 PHP-SDK 通过 Facebook Graph API 批量请求 ======== // 将方法调用保存到数组中 $queries = array( array('method' => 'GET', 'relative_url' => '/'.$user), array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'), array( 'method' => 'GET', 'relative_url' => '/'.$user.'/groups'), array('method' => 'GET', 'relative_url' => '/'.$user. '/喜欢'), );
// POST your queries to the batch endpoint on the graph.
try{
$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
}catch(Exception $o){
error_log($o);
}
//Return values are indexed in order of the original array, content is in ['body'] as a JSON
//string. Decode for use as a PHP array.
$user_info = json_decode($batchResponse[0]['body'], TRUE);
$friends_list = json_decode($batchResponse[1]['body'], TRUE);
$groups = json_decode($batchResponse[2]['body'], TRUE);
$pages = json_decode($batchResponse[3]['body'], TRUE);
//========= 使用 PHP-SDK 通过 Facebook Graph API 的批量请求结束 =====
if(isset($_POST['submit_x'])){
if($_POST['message'] || $_POST['link'] || $_POST['picture']) {
$body = array(
'message' => $_POST['message'],
'link' => $_POST['link'],
'picture' => $_POST['picture'],
'name' => $_POST['name'],
'caption' => $_POST['caption'],
'description' => $_POST['description'],
);
$batchPost=array();
$i=1;
$flag=1;
foreach($_POST as $key => $value) {
if(strpos($key,"id_") === 0) {
$batchPost[] = array('method' => 'POST', 'relative_url' => "/$value/feed", 'body' => http_build_query($body));
if($i++ == 50) {
try{
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');
}catch(FacebookApiException $e){
error_log($e);
echo("Batch Post Failed");
}
$flag=0;
unset($batchPost);
$i=1;
}
}
}
if(isset($batchPost) && count($batchPost) > 0 ) {
try{
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');
}catch(FacebookApiException $e){
error_log($e);
echo("Batch Post Failed");
}
$flag=0;
}
}
else {
$flag=2;
}
}
} ?>