Facebook APIã䜿çšããªãããã¯ãšãªã®æé©åã®ããã®ããã€ãã®ã¬ã·ããèç©ããŸãããã¹ã¯ãªããã®é床ãäžãããã®æ°ãšãªãœãŒã¹æ¶è²»ãåæžããæ¹æ³ã§ãã
ãã®èšäºã§æŠèª¬ããã¡ãœããã¯ãFacebook APIã§ã®ã¿æ©èœããŸãã ããããAPIãæäŸããä»ã®ãµãŒãã¹ã«é©çšã§ããå¯èœæ§ãããããšãé€å€ããŸããã
ã¯ããã«
ããããããŠãŒã¶ãŒã®ããŒã¿ãšåœŒã®FacebookãœãŒã·ã£ã«ã°ã©ãã§æ©èœããWebã¢ããªã±ãŒã·ã§ã³ã®ã¢ã€ãã¢ãããã§ãããã ãããŠã ããã¥ã¡ã³ããç¹°ãè¿ãéããããã€ãã®ã¹ã¯ãªãããäœæããããšããã§ããŸããã ããããèªçæ¥ãã¬ãŒã³ããã¢ããªã±ãŒã·ã§ã³ãšããæåã«ãã¹ãããšã¯ããŠãŒã¶ãŒã®å人ãšãã®èªçæ¥ã®ãªã¹ããååŸããããšã§ãã
ãã¹ãŠãã·ã³ãã«ã§ããããªãã¯æãïŒ
- ãŠãŒã¶ãŒãæ¿èªããŸãïŒãŠãŒã¶ãŒIDãšaccess_tokenãååŸããŸããããã¯ä»åŸã®ãªã¯ãšã¹ãã§äœ¿çšããŸãïŒã
- ç§ã¯åœŒã®å人ã®ãªã¹ããååŸããŸãïŒåºåã§ã¯å人ã®IDãšååã®é åããããŸãïŒã
- åID ã«ã€ããŠããŠãŒã¶ãŒããŒã¿ã®ãªã¯ãšã¹ããè¡ããŸã ïŒ èªçæ¥ããŒã¿ãååŸããã«ã¯ãæ¿èªäžã«friends_birthdayããŒããã·ã§ã³ããªã¯ãšã¹ãããå¿ èŠããããŸãïŒã
ã¹ã¯ãªããã¯ãµãŒããŒåŽã§åäœããããããŠãŒã¶ãŒã«è¡šç€ºããåã«ãã¹ãŠã®ããŒã¿ãååŸããå¿ èŠããããŸãã ãããŠãããã§ããªãã¯æåã§æãéèŠãªåé¡ã«çŽé¢ããŠããŸãïŒAPIãžã®1ã€ã®ãªã¯ãšã¹ãã¯1ã2ç§ããããŸãã ãããã£ãŠããã¹ãŠã®åéã®ãªã¹ãã®ã¿ãååŸããã«ã¯ã2ã4ç§ïŒåéã®æ°ã«ããïŒãå¿ èŠã§ãã
10人ã®å人ã®èªçæ¥ã衚瀺ããã¹ã¯ãªãããå®è¡ãããæéãèšç®ããŠã¿ãŸãããã
<?php $start = microtime(true); $app_id = "YOUR_APP_ID"; $app_secret = "YOUR_APP_SECRET"; $my_url = "YOUR_URL"; session_start(); $code = $_REQUEST["code"]; if(empty($code)) { $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' . $app_id . '&redirect_uri=' . urlencode($my_url) . '&scope=user_birthday,friends_birthday&state=' . $_SESSION['state']; echo("<script> top.location.href='" . $dialog_url . "'</script>"); } if($_REQUEST['state'] == $_SESSION['state']) { $token_url = 'https://graph.facebook.com/oauth/access_token?' . 'client_id=' . $app_id . '&redirect_uri=' . urlencode($my_url) . '&client_secret=' . $app_secret . '&code=' . $code; $response = file_get_contents($token_url); $params = null; parse_str($response, $params); $graph_url = "https://graph.facebook.com/me?access_token=" . $params['access_token']; $user = json_decode(file_get_contents($graph_url)); $uid = $user->id; // $graph_url = 'https://graph.facebook.com/'.$uid.'/friends?limit=10&access_token=' . $params['access_token']; $friends = json_decode(file_get_contents($graph_url)); $time = microtime(true) - $start; printf(' %.4F .<br/>', $time); $n = sizeof($friends->data); for ($i = 0; $i < $n; $i++) { $graph_url = 'https://graph.facebook.com/' . $friends->data[$i]->id . '?access_token=' . $params['access_token']; $friend_data = file_get_contents($graph_url); // json //$friend = json_decode($friend_data); //echo($i.' '.$friend->name.' - '.$friend->birthday); } } else { echo("The state does not match. You may be a victim of CSRF."); } $time = microtime(true) - $start; printf(' %.4F .', $time);
ãããè¡ãã«ã¯13ç§ããããŸããïŒ ããªãã®ãŠãŒã¶ãŒã¯ãããªã«é·ãæéåŸ ã¡ãŸããïŒ ããã¯æããŸãã ãããŠãå人ã®æ°ãæ°çŸäººããããã¯æ°å人ã§ãã枬å®ãããå Žå...
æé©åãå§ããŸãããã
æ¹æ³1ïŒå¿ é ãã£ãŒã«ãã®ã¿ãèªã¿åã
ãŠãŒã¶ãŒããŒã¿ïŒGraph APIã®ãŠãŒã¶ãŒãªããžã§ã¯ãïŒãèŠæ±ãããšãFacebookã¯ããã©ã«ãã§ãã¢ã¯ã»ã¹ã§ãããã¹ãŠã®ãã£ãŒã«ããæž¡ããŸãã ã©ã®ãã£ãŒã«ããè¿ãå¿ èŠããããã瀺ãããšã«ãããåé·ãªæ å ±ãåãé€ãããšãã§ããŸãã ãããè¡ãããã«ãèŠæ±ã¯fieldsãã©ã¡ãŒã¿ã䜿çšããŸãããã®ãã©ã¡ãŒã¿ã§ã¯ãå¿ é ãã£ãŒã«ãã¯ã«ã³ãã§åºåãããŠããŸãã ããšãã°ããã®å Žåããªã¯ãšã¹ãã¯{user_id}ïŒFields = idãnameãbirthday
- [ ã¢ã¯ã»ã¹ããŒã¯ã³ã®ååŸ ]ãã¯ãªãã¯ãã [ãŠãŒã¶ãŒããŒã¿ã®ã¢ã¯ã»ã¹èš±å¯]ããã³[ ãã¬ã³ãããŒã¿ã®ã¢ã¯ã»ã¹ èš±å¯]ã®ãã¹ãŠã®ãã§ãã¯ããã¯ã¹ããªã³ã«ããŠã ã°ã©ãAPIãšã¯ã¹ãããŒã©ãŒã§å¿ èŠãªã¢ã¯ã»ã¹èš±å¯ãèš±å¯ããŸãã
- httpsïŒgraph.facebook.com/ã®åŸã«ãããã¯ã¹ã«ç§ãå ¥åããŠãã ãã
- åã®åºåããªã¯ãšã¹ãmeãšæ¯èŒããŸããïŒFields = idãnameãbirthday
fieldsãã©ã¡ãŒã¿ã䜿çšããã¯ãšãªã¯ãå¿ èŠãªããŒã¿ã®ã¿ãè¿ããããé«éã§ãã å°ããããããããã¯ãŒã¯ãä»ããŠããé«éã«éä¿¡ãããŸãã ãŸããããé«éã«åŠçããã䜿çšããã¡ã¢ãªãå°ãªããªããŸãã
ãã®æé©åæ¹æ³ã¯ãå šäœçãªã©ã³ã¿ã€ã ã«å®éã«ã¯åœ±é¿ãäžããŸãããã決ããŠå¿ããŠã¯ãªããªãæ¹æ³ã§ãã åãªã¯ãšã¹ãã®å¿ é ãã£ãŒã«ãã®ã¿ãèªãã§ãã ããïŒ
æ¹æ³2ïŒ1ã€ã®ã¯ãšãªã§è€æ°ã®ãªããžã§ã¯ãããããŒã¿ãèŠæ±ãã
idsãã©ã¡ãŒã¿ãŒã䜿çšãããšãåä¿¡ããè€æ°ã®ãªããžã§ã¯ããéžæã§ããŸãã ãã®å Žåã fieldsãã©ã¡ãŒã¿ãŒã䜿çšããŠããªããžã§ã¯ãã®å¿ é ãã£ãŒã«ãã®ã¿ãå¶éã§ããŸãïŒãªããžã§ã¯ãã¯åãã¿ã€ãã§ãªããã°ãªããŸããïŒã ããšãã°ãã¯ãšãªïŒIds = 4,501012028ã䜿çšãããšã2人ã®ãŠãŒã¶ãŒããäžåºŠã«ãªãŒãã³ããŒã¿ãååŸã§ããŸã;ïŒ
ã«ã³ãã§åºåã£ãŠæå®ã§ãããªããžã§ã¯ãã®æ°ã¯ãæ倧ã®é·ãURLã«ãã£ãŠã®ã¿å¶éãããŸãã åæã«ãèŠæ±ã«å¯Ÿããå¿çã®ãµã€ãºãå¢å ããããšãå¿ããªãã§ãã ããã ãããã£ãŠã1ã€ã®èŠæ±ã§ãªããžã§ã¯ãã®æ°ãå¶éããããšã¯åççã§ãã ããšãã°ã1çªç®ãš2çªç®ã®æ¹æ³ã䜿çšãããšãçãã¯æ¬¡ã®ããã«ãªããŸãã
ãã®äŸã§ãã®ã¡ãœããã䜿çšããã«ã¯ãã¹ã¯ãªãããæžãçŽãå¿ èŠããããŸãã
$ n = sizeofïŒ$ friends-> dataïŒ; $ graph_url = 'https://graph.facebook.com/?ids='; forïŒ$ i = 0; $ i <$ n; $ i ++ïŒ{ $ graph_urlã= $ friends-> data [$ i]-> idã 'ã'; } $ graph_url = substrïŒ$ graph_urlã0ã-1ïŒ; $ graph_urlã= 'ïŒaccess_token ='ã $ params ['access_token']; $ friend_data = file_get_contentsïŒ$ graph_urlïŒ; // JSONã¬ã¹ãã³ã¹ããã³ãŒãããããŒã¿ãåºåããŸã
ããã§ã10åã®ãªã¯ãšã¹ãã®ä»£ããã«ããããã1ã€ã ãã䜿çšããã¹ã¯ãªããã®å®è¡æéã¯4ã5ç§ã«ççž®ãããŸããã ãã ãã10人ã ãã§ãªãããã¹ãŠã®å人ã®ããŒã¿ãååŸããå¿ èŠããããŸãã
æ¹æ³3ïŒãã£ã«ã¿ãªã³ã°ãšããŒãžããŒã·ã§ã³ã䜿çšãã
ããŒãžããŒã·ã§ã³ã¯ãæ å ±ããããŒãžãã«å解ããã ãã§ãã limitãã©ã¡ãŒã¿ãŒã䜿çšããŠããã£ã10人ã®å人ã«é¢ããæ å ±ãåãåããŸããã åãéšåã®æ®ãã®æ å ±ãååŸããããã«ã offsetãã©ã¡ãŒã¿ãŒãååšããŸãã ããšãã°ãæåã®10人ã®åäººïŒ me / friendsïŒLimit = 10ïŒoffset = 0ã2çªç®ã®10人ã®åäººïŒ me / friendsïŒLimit = 10ïŒoffset = 10ãªã©
次ã®ããã«ã¹ã¯ãªãããæé©åã§ããŸãã
- ãµãŒããŒãžã®Ajaxãªã¯ãšã¹ããå«ãã¯ã©ã€ã¢ã³ãéšåãäœæããŸãã
- æåã®10人ã®å人ã®ããŒã¿ãååŸããŠè¡šç€ºããŸãã
- ããŒã¿ã衚瀺ãããŠããéã«ãããŒã¿ã®æ¬¡ã®ãéšåããããŒãããŸãã
ããã§ãæ å ±ã衚瀺ããããã«ããªã¯ãšã¹ãã®ãã¹ãŠã®éšåãåŠçããããŸã§åŸ ã€å¿ èŠããªããªããŸããã åæã«ãæåã®éšåã«ã€ããŠã¯ã1åã®èŠæ±ã§ååãªéã®ããŒã¿ãæ¢ã«åãåããŸãã
1åã®ãªã¯ãšã¹ãã§ïŒãŸãã¯äžåºŠã«ãã¹ãŠïŒæ°ååã®ããŒã¿ãååŸã§ãããããæ·±å»ãªã¡ãœããã«ç§»ããŸãããã
æ¹æ³4ïŒFQLã¯ãšãªã®æ§ç¯ïŒFacebookã¯ãšãªèšèªïŒ
FQLã䜿çšãããšãããŒã¿ã®ã¯ãšãªã«SQLã¹ã¿ã€ã«ã®ã€ã³ã¿ãŒãã§ã€ã¹ã䜿çšã§ããŸãã ãã®å©ãã«ãããGraph APIãä»ããŠå©çšã§ããªãããã€ãã®ã¯ãšãªãå®è£ ããã®ãç°¡åã«ãªããŸãã èŠæ±ãã©ãŒã ã¯æ¬¡ã®ãšããã§ããSELECT [ãã£ãŒã«ã] FROM [ããŒãã«] WHERE [æ¡ä»¶] ãã ããFQLã«ã¯å€ãã®å¶éããããŸãïŒSQLãšæ¯èŒããå ŽåïŒã ãããã£ãŠãããšãã°ã FROMã§ã¯1ã€ã®ããŒãã«ã®ã¿ã䜿çšã§ããŸãã ãã ãããã¹ããããã¯ãšãªã䜿çšã§ããŸãã FQLã§ã¯ãè«çæŒç®åã ORDER BYããã³LIMITæ§æãããã³ãã®ä»ã®æŒç®åã䜿çšã§ããŸãã
ãŠãŒã¶ãŒã®ååãšç幎ææ¥ãååŸããã«ã¯ããŠãŒã¶ãŒããŒãã«ã§FQLã¯ãšãªãäœæããå¿ èŠããããŸãã ããšãã°ãGraph API Explorerã§ã¯ãããã¯ã¯ãšãªfqlïŒQ = SELECT uidãnameãbirthday_date FROM user WHERE uid = meïŒïŒã«ãªããŸãã ãããŠãå人ã®idã®ãªã¹ããååŸããããã«ãããŒãã«friend fqlïŒQ = SELECT uid2 FROM friend WHERE uid1 = meïŒïŒãžã®ã¯ãšãªã䜿çšããŸãã
ãµãã¯ãšãªã䜿çšããŠã2ã€ã®ã¯ãšãªã1ã€ã«çµåããŠã¿ãŸãããã fqlïŒQ = SELECT uidãnameãbirthday_date FROM user WHERE uid INïŒSELECT uid2 FROM friend WHERE uid1 = meïŒïŒïŒ ã äœåãã®ãªã¯ãšã¹ãã®ä»£ããã«ãå¿ èŠãªãã¹ãŠã®æ å ±ããã£ã1ã€ã§ååŸããŸããïŒ
//ãŠãŒã¶ãŒã®åéãšãã®èªçæ¥ã®ãªã¹ããååŸããŸã $ graph_url = 'https://graph.facebook.com/fql?q=SELECT uidãnameãbirthday_date FROM user WHERE uid INïŒSELECT uid2 FROM friend WHERE uid1 ='ã$ uidã 'ïŒïŒaccess_token =' $ params ['access_token']; $ frnds = file_get_contentsïŒ$ graph_urlïŒ; // JSONã¬ã¹ãã³ã¹ããã³ãŒãããããŒã¿ãåºåããŸã
FQLã¯ãšãªãæé©åããæ¹æ³ã¯å€æ°ãããŸãã ããã¥ã¡ã³ãã®ãã¹ãŠã®è¡šã調ã¹ãŸãã ãã ããFQLã¯ã¯ãšãªæé©åã®æé«å³°ã§ã¯ãããŸããã
ãŠãŒã¶ãŒã®ææ°ã®æçš¿ã圌ã®ãã¥ãŒã¹ãã£ãŒãããã¹ãŠã®æªèªéç¥ãæ°ããã¡ãã»ãŒãžã®ãªã¹ããååŸããå¿ èŠããããšæ³åããŠãã ããã ãããŠãããããã¹ãŠãããã³ãã以äžã®ããšãè¿ éã«è¡ãå¿ èŠããããŸãã ãã¡ãããã¯ãšãªãéåæã«ããããšã§ã¯ãšãªã䞊ååã§ããŸãã ããããããã¯ç§ãã¡ã®ããæ¹ã§ã¯ãããŸããã
æ¹æ³5ïŒããããªã¯ãšã¹ã
Graph APIã䜿çšãããšãFQLã¯ãšãªã§ãã£ãŠãã1ã€ã®ã¯ãšãªããã®ããŒã¿ãåŠçã§ããŸãã ããããªã¯ãšã¹ãã䜿çšãããšã1ã€ã®ãªã¯ãšã¹ãã§ãµãŒããŒã«éä¿¡ã§ããŸã-ããã€ãã®ç°ãªããªã¯ãšã¹ãã®ããã¯ã å¯äžã®å¶éã¯ã1åã®ãããåŒã³åºãã§æ倧20ãªã¯ãšã¹ãã§ãã èŠæ±ã¯ãGETãPOSTãããã³DELETEã§ãã
ããšãã°ããªã¯ãšã¹ãã®ãããã±ãŒãžãã¯æ¬¡ã®ãšããã§ãã
- ãªã³ã¯ä»ãã®æ°ããã¹ããŒã¿ã¹ãéä¿¡ããŸãã
- ææ°ã®ã¹ããŒã¿ã¹ã«é¢ããæ å ±ãååŸããŸãïŒæ°èŠã¯èæ ®ãããŸããïŒã
- æåŸã®ã¹ããŒã¿ã¹ãšåæ§ã«ã³ã¡ã³ããæžããŸãã
$ batched_request = '[{"method"ïŒ "POST"ã "relative_url"ïŒ "me / feed"ã "body"ïŒ "ã¡ãã»ãŒãž=æ°ããæçš¿ã¯è¿æ¥å ¬éïŒãªã³ã¯= httpïŒ//habrahabr.ru/"}'; $ batched_requestã= 'ã{"method"ïŒ "GET"ã "name"ïŒ "get-post"ã "relative_url"ïŒ "me / feedïŒlimit = 1"}'; $ batched_requestã= 'ã{"method"ïŒ "POST"ã "relative_url"ïŒ "{result = get-postïŒ$ãdata.0.id} / likes"}} ;; $ batched_requestã= 'ã{"method"ïŒ "POST"ã "relative_url"ïŒ "{result = get-postïŒ$ãdata.0.id} / comments"ã "body"ïŒ "ã¡ãã»ãŒãž=æ°ããèªåã³ã¡ã³ã" } '; $ batched_requestã= ']'; $ url = 'https://graph.facebook.com/'; $ param = arrayïŒïŒ; $ param ['access_token'] = $ params ['access_token']; $ param ['batch'] = $ batched_request; $ ch = curl_initïŒïŒ; curl_setoptïŒ$ chãCURLOPT_URLã$ urlïŒ; curl_setoptïŒ$ chãCURLOPT_RETURNTRANSFERãtrueïŒ; curl_setoptïŒ$ chãCURLOPT_POSTFIELDSã$ paramïŒ; $ ret = curl_execïŒ$ chïŒ; //å¿çåŠç ifïŒ$ ret == 'false'ïŒecho '= false ='; curl_closeïŒ$ chïŒ;
ããããªã¯ãšã¹ãã¯POSTã¡ãœããã䜿çšããŠéä¿¡ããå¿ èŠããããããäžèšã®ã³ãŒãã«ã¯cURLãé¢ä¿ããŠããŸãã ãŸãããã®å©ãã«ãããèµ·ãããããšã©ãŒã®åŠçã容æã«ãªããŸãã
ãã®èšäºã§ã¯ããããèŠæ±ã䜿çšãããã¹ãŠã®æ¹æ³ã«ã€ããŠèª¬æããŠããŸããã ãããã®è©³çŽ°ã«ã€ããŠã¯ã ããã¥ã¡ã³ããã芧ãã ããã