FacebookのAPIを使って友人の公開情報を取得してみる
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Language" content="ja"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>友人のユーザーIDを取得</title> <script type="text/javascript"> </script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript"> window.fbAsyncInit = function () { FB.init({ appId: '1234567890',//このIDはあなたのもにに書き換えてください cookie: true, oauth: true }); //ログインチェック FB.getLoginStatus(function (resp) { if (resp.authResponse) { getdata(); } else { $("#result").text("ログインしてください。"); } }); // ログイン処理 $("#login").click(function () { FB.login(function (resp) { if (resp.authResponse) { getdata(); } },{scope: 'user_about_me,user_birthday,user_hometown,user_location,user_likes,user_work_history'} ); }); // APIからデータ取得 var getdata = function () { // 友達リストを取得 FB.api('/me/friends', function (resp) { var htm = '<br>友達リスト:<ul>'; for (var i = 0; i < resp.data.length; i++) { htm += '<li>' + resp.data[i].name + ':' + resp.data[i].id + '</li>'; } htm += '</ul>'; $("#result").html(htm); }); } }; $(function () { (function () { var e = document.createElement('script'); e.src = document.location.protocol + '//connect.facebook.net/ja_JP/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); } ()); }); </script> </head> <body> <h1>Facebookにログインして友人のIDを取得</h1> <div id="fb-root"></div> <p><button id="login">Facebookにログイン</button></p> <div id="result"></div> </body> </html>
<?php $userid = $_GET["userid"]; require '../src/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => '1234567890', 'secret' => '06069a5f250fed112345678900', )); // Get User ID $user = $facebook->getUser(); if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $user = null; } } // Login or logout url will be needed depending on current user state. if ($user) { $logoutUrl = $facebook->getLogoutUrl(); } else { $loginUrl = $facebook->getLoginUrl(); } // This call will always work since we are fetching public data. $friends = $facebook->api('/' . $userid); ?> <!doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>php-sdk</title> <style> body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; } h1 a { text-decoration: none; color: #3b5998; } h1 a:hover { text-decoration: underline; } </style> </head> <body> <h1>友人の公開情報</h1> <h3>Public profile of Friend</h3> <?php echo $friends['name']; ?> <pre><?php print_r($friends); ?></pre> </body> </html>