2011年6月17日 星期五

facebook requests Dialog 使用 (送出邀請朋友)

facebook 文件 http://developers.facebook.com/docs/reference/dialogs/requests/
運用FQL跟requests Dialog來傳送邀請給線上好友

實作範例教學:
PHP

include('inc/facebook.php');
$facebook = new Facebook(array(
               'appId'  => $facebook_app_key,
               'secret' => $facebook_app_secret,
               'cookie' => true
            ));
$fb_id = $facebook->getUser();
if( !$fb_id ){
    echo 'error'; exit;
}

try{
    $aFriends = $facebook->api(array(
        'method' => 'fql.query',
        'query' => 'select uid from user where online_presence in("active") and uid IN (SELECT uid1 FROM friend WHERE uid2 = '.$fb_id.') order by name'
    ));
}catch(Exception $e){ }

$aF = array();
foreach( $aFriends as $k => $v ){
    $aF[] = $v['uid'];
}


HTML

<div id="fb-root"></div>
<script src="http://connect.facebook.net/zh_TW/all.js"></script>
<script>
FB.init({
    appId  : '<?php echo h($facebook_app_id); ?>',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
});
FB.ui({ method: 'apprequests'
    ,message: '可自定文字'
    ,filters: "[{name: '線上好友', user_ids: [<?php echo join(',',$aF); ?>]}]"
    ,title:"可自定標題"
    ,data:o
},function(r){ //回傳request_id
    alert(r.request_ids[0]})
    });
});
</script>

沒有留言:

張貼留言