首先先宣告告queryFQL Function 簡介呼叫程序
function queryFQL(sql , callBack){ FB.api({method:'fql.query',query:sql}, callBack); }
取得所有群組名稱
queryFQL('select flid , name from friendlist where owner=me()' , function(response){ console.log(response); });
取得好友與所在群組資料
queryFQL('SELECT uid, flid FROM friendlist_member WHERE flid IN (SELECT flid FROM friendlist WHERE owner=me())' , function(response){ console.log(response); });
取得好友的詳細資料
queryFQL('SELECT name,pic_square, uid from user where uid in (SELECT uid2 FROM friend WHERE uid1 = me())', function(response){ console.log('user-data',response); })
如果要確知道要取得那些資料,可以使用multiquery。減少request的發送次數
先宣告multiQueryFQL Function
var multiQueryFQL = function(sql , callBack){ FB.api({method:'fql.multiquery',queries:sql} , callBack); }
同時取得好友與分類名稱
multiQueryFQL([ 'select flid , name from friendlist where owner=me()', 'SELECT uid, flid FROM friendlist_member WHERE flid IN (SELECT flid FROM friendlist WHERE owner=me())', 'SELECT name,pic_square, uid from user where uid in (SELECT uid2 FROM friend WHERE uid1 = me())'], function(response){};
第一個參數型態為Array,存放所有FQL Query指令。
回傳資料也為Array。對應各個FQL結果
至於取得的User詳細資料。可轉換為User['名稱'] = UID ; 的儲存陣例,方便查詢
以上面使用multiQueryFQL查詢為例,第三個FQL查詢回傳Use的詳細資料
var users = response[2].fql_result_set ; var userInfo = {} ; for(var uid in users){ userInfo[users[uid].uid.toString()] = users[uid].name ; }
取得使用者圖片,可以以透過下列網址取得
http://graph.facebook.com/[user id]/picture
如果要將每個friend加入相對應的群組,方便資料查詣。則可以做個簡單的資料轉換。
var groupInfo = response[0].fql_result_set ; var friendList = response[1].fql_result_set ; if(!groupInfo && !friendList && !users) return ; for (var gid in groupInfo){ if(!groupInfo[gid].user) groupInfo[gid].user = [] ; for(var fid in friendList){ if(groupInfo[gid].flid === friendList[fid].flid){ groupInfo[gid].user.push(users[uid].uid); delete friendList[fid]; } } } //最後groupInfo的資料格式為 [{flid:'0001',name:'title-name',user:['0001','0002']},{flid:'0002',name:'title-name',user:[xxx,xxx,]}]
沒有留言:
張貼留言