FacebookのAPIを使って友人の公開情報を見てみませう。

そのためには友人のユーザーIDが必要です。

無ければ友人から聞いてみてください。

教えてくれない場合は試しにザッカーバーグ氏のIDを使ってみましょう。IDは"4"です。

因みに、サッカー日本代表は、"316796965309"。

(全角・半角チェックしていません、必ず半角で入力してくださいね)

ユーザーID  




友人のユーザーIDを取得するコード

Facebookのアプリを開発するためにはDeveloper登録をして、アプリの情報を登録し、App_IDやSceretを取得する必要があります。

取得までの流れはFacebookのアプリをつくるための準備参照。

コード、jQueryを使ってます。

<!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>





ユーザーIDを使って公開情報を取得するPHPコード

GieHubのFacebookディレクトリから「facebook-php-sdk」をダウンロード。

具体的に使うのは、examplesフォルダー内のexample.phpとsrcフォルダー内のファイル一式です。

example.php内のsrcファイルの参照パスは環境に合わせて書き換えてください。

以下は公開情報を取得する最小コードです。赤い部分は適当に書き換えてください。

<?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>