目次 †
内容 †
作成手順 †
新規追加 †
- Facebookにログインして次のURLにアクセス
- ページカテゴリを選択肢、プロフィール等を適宜入力
アクセス方法 †
- Facebook個人ページの左メニューに表示されるFacebookページ名をクリック
- 「いいね!」の数が一定数を超えたらシンプルなURLが利用できる
独自タブの追加 †
- 独自タブとは、Facebookページの中にiframeを使って表示する独自のWebページのこと
- FacebookアプリとしてWebページを登録し、それをFacebookページに登録する
コンテンツの用意 †
- SSLに対応したWebサーバに、Facebookで表示するiframe用コンテンツをアップする
開発者登録 †
アプリ追加 †
- 開発者用ページにアクセスし、「新しいアプリケーションを作成」をクリック
- 携帯電話かクレジットカードを使ってアカウントの認証を行う
- アプリの情報を登録、その際、iframeで表示するURL(SSL)も入力する
- アプリ設定を入力する
- 「Facebook上のアプリ」には、iframeで表示するページURLのディレクトリを指定
- 「ページタブ」には、iframeで表示するページURLを指定
独自タブの追加 †
- アプリページにアクセスし、左メニューにある「マイページに追加」をクリック
PHP-SDKの使用 †
- Facebookから提供されているSDKを利用することで、Facebookアプリでアクセスしたユーザのプロフィールといった情報が取得できる
- 標準の状態では、アクセスしたユーザが「いいね!」を押したかどうか程度しかわからないが、ユーザが許可すれば詳細なプロフィール情報が取得できる。
参考 †
利用手順 †
- PHP-SDKをgithubからダウンロード
- iframeのPHPスクリプトにrequireして利用する
サンプルスクリプト(シンプル) †
- 「いいね」の有無だけを知りたい場合はgetSignedRequest()を利用する
- appIdとsecretはアプリ管理ページにて確認
<?php
require_once( "/path/to/facebook-php-sdk/src/facebook.php" );
$facebook = new Facebook(array(
'appId' => '******', // 適宜調整
'secret' => '******', // 適宜調整
));
print_r( $facebook->getSignedRequest() );
- 結果は次の通り
Array
(
[algorithm] => HMAC-SHA256
[page] => Array
(
[id] => 279276982144078
[liked] => 1
[admin] => 1
)
[user] => Array
(
[country] => jp
[locale] => ja_JP
[age] => Array
(
[min] => 21
)
)
)
サンプルスクリプト †
- SDKに同梱されていたサンプルスクリプト(一部不要部分を除去)
- appIdとsecretはアプリ管理ページにて確認
<?php
/**
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
require './facebook-php-sdk/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '*************', // 適宜調整
'secret' => '*************', // 適宜調整
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
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();
}
?>
<!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>php-sdk</h1>
<?php if ($user): ?>
<a href="<?php echo $logoutUrl; ?>">Logout</a>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
- このページを表示すると、プロフィール情報アクセスの許可が求められる。

- $user_profile = $facebook->api('/me'); でプロフィール情報が取得できる
|