*Facebook應用程式框架:
下載官方提供的工具及範例,建立PHP的應用程式的框架。
使用者點進應用程式後,會出現授權請求,當用戶同意授權後,會列出個人資料。
1.我們需要使用Facebook提供的工具,才能整合Facebook的功能。
首先我們要下載Facebook提供的PHP SDK。
下載網址:
http://github.com/facebook/php-sdk
按ZIP下載檔案
2.解壓縮檔案後,可以看到3個資料夾,請打開「src」資料夾,會看到以下檔案:
facebook.php:就是SDK本身。PHP程式透過你內建的功能,跟Facebook平台溝通。
base_facebook.php
fb_ca_chain_bundle.crt:安全性憑證。
3.將「src」資料夾複製到本機的網頁根目錄(C:\AppServ\www)下。
然後在「C:\AppServ\www」下建立一個資料夾為「ch3」,
並在ch3內建立新PHP文件,命名為「index.php」,輸入以下內容:
*連結Facebook及存取個人資料:
<?php
require '../src/facebook.php';
//建立FACEBOOK SDK 物件
$facebook = new Facebook(array(
'appId' => '408505229167347',// 應用程式ID
'secret' => '9663e5af90bd9508bd8f2ff0fef7bd6d', // 應用程式密鑰
'cookie' => true,
));
//嘗試取得使用者ID
$uid = $facebook->getUser();
//設定跳回的應用程式網址
// $redirectUrl = 'http://apps.facebook.com/helloworld_tw/ch7_d/';
$redirectUrl = 'http://apps.facebook.com/ericwebtw/ch3/';
//設定跳回的應用程式網址
$loginUrl = $facebook->getLoginUrl(
array('scope' => 'publish_stream',//新版的授權參數
'redirect_uri'=>$redirectUrl,//回傳網址
'canvas' => 1, 'fbconnect' => 0
));
//檢查是否存在使用者ID
if($uid) {
//有使用者ID,嘗試取得個人資料
try {
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$uid = null;
$me = null;
}
}else{
//沒有使用者ID,導引到登入頁面
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
//已取得使用者資料,利用GRAPH API 存取使用者對應用程式的授權,
$permissions = $facebook->api("/me/permissions");
//檢查授權清單,以避免使用者缺乏識別授權項目
if(!array_key_exists('publish_stream', $permissions['data'][0]) ) {
//缺乏識別授權項目,導引到登入頁面
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
?>
------------
註解:
getUser()
取得你的Facebook會員ID。
api('/me')
api方法就是使用Graph API功能,裡面輸入參數「'/me'」,
就可以取得個人資料。
getLoginUrl()
可以取得你的應用程式的登入連結,同時你可以存入不同參數,
設定你的登入需求。
其中主要參數是「scope」,是指授權方式,應用程式想要進行某些動作
(張貼塗鴉牆、傳email等),都需要用戶的相對應的授權。
例如:
publish_stream
發佈訊息到使用者及使用者好友的塗鴉牆上。
------------
4.連結網址:
http://apps.facebook.com/ericwebtw/ch3/
網頁打開後,會出現「授權請求」的畫面。
你可以先不用同意,因為上面的程式碼,
主要是做登入處理,即使同意了授權,列出的頁面是空白的。
5.在同一支程式後面輸入下列內容:
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>PHP-facebook 框架</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
//include_once "../_menu.php";
?>
<?php if ($me): ?>
<img src="https://graph.facebook.com/<?php echo $uid; ?>/picture">
<h4><?php echo $me['name']; ?> 個人資料</h4>
<pre><?php print_r($me); ?></pre>
<?php else: ?>
<strong>未連結!!</strong>
<?php endif ?>
</body>
</html>
6.總結:連結Facebook及存取個人資料
完整程式如下:
<?php
require '../src/facebook.php';
//建立FACEBOOK SDK 物件
$facebook = new Facebook(array(
'appId' => '408505229167347',// 應用程式ID
'secret' => '9663e5af90bd9508bd8f2ff0fef7bd6d', // 應用程式密鑰
'cookie' => true,
));
//嘗試取得使用者ID
$uid = $facebook->getUser();
//設定跳回的應用程式網址
// $redirectUrl = 'http://apps.facebook.com/helloworld_tw/ch7_d/';
$redirectUrl = 'http://apps.facebook.com/ericwebtw/ch3/';
//設定跳回的應用程式網址
$loginUrl = $facebook->getLoginUrl(
array('scope' => 'publish_stream',//新版的授權參數
'redirect_uri'=>$redirectUrl,//回傳網址
'canvas' => 1, 'fbconnect' => 0
));
//檢查是否存在使用者ID
if($uid) {
//有使用者ID,嘗試取得個人資料
try {
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$uid = null;
$me = null;
}
}else{
//沒有使用者ID,導引到登入頁面
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
//已取得使用者資料,利用GRAPH API 存取使用者對應用程式的授權,
$permissions = $facebook->api("/me/permissions");
//檢查授權清單,以避免使用者缺乏識別授權項目
if(!array_key_exists('publish_stream', $permissions['data'][0]) ) {
//缺乏識別授權項目,導引到登入頁面
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>PHP-facebook 框架</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
//include_once "../_menu.php";
?>
<?php if ($me): ?>
<img src="https://graph.facebook.com/<?php echo $uid; ?>/picture">
<h4><?php echo $me['name']; ?> 個人資料</h4>
<pre><?php print_r($me); ?></pre>
<?php else: ?>
<strong>未連結!!</strong>
<?php endif ?>
</body>
</html>
登入Facebook後,連結網址:
http://apps.facebook.com/ericwebtw/ch3/
輸出結果:
照片
Eric Lin 個人資料
Array
(
[id] => 100001058754902
[name] => Eric Lin
[first_name] => Eric
[last_name] => Lin
[link] => http://www.facebook.com/profile.php?id=100001058754902
[gender] => male
[timezone] => 8
[locale] => zh_TW
[verified] => 1
[updated_time] => 2012-03-02T17:19:19+0000
)
--------------------------------------
html碼如下:
<html xmlns:fb="http://www.facebook.com/2008/fbml"><head>
<title>PHP-facebook 框架</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<img src="https://graph.facebook.com/100001058754902/picture">
<h4>Eric Lin 個人資料</h4>
<pre>Array
(
[id] => 100001058754902
[name] => Eric Lin
[first_name] => Eric
[last_name] => Lin
[link] => http://www.facebook.com/profile.php?id=100001058754902
[gender] => male
[timezone] => 8
[locale] => zh_TW
[verified] => 1
[updated_time] => 2012-03-02T17:19:19+0000
)
</pre>
</body></html>
--------------------------------------
下列User參數解釋:
http://developers.facebook.com/docs/reference/api/user/
Array
(
[id] => 100001058754902
[name] => Eric Lin
[first_name] => Eric
[last_name] => Lin
[link] => http://www.facebook.com/profile.php?id=100001058754902
[gender] => male
[timezone] => 8
[locale] => zh_TW
[verified] => 1
[updated_time] => 2012-03-02T17:19:19+0000
)
--------------------------------------
PHP SDK:
http://developers.facebook.com/docs/reference/php/
======================================================================
*PHP免費空間(支援Curl,才能測試Facebook Graph API)
http://www.freewebhostingarea.com/
您或許對下列文章會感興趣:
建立新的Facebook應用程式
加入Facebook應用程式開發人員
Facebook 預約發文功能,可以在你設定的時間自動發表文章!