[教學] Facebook API PHP SDK 基本篇. Facebook API PHP SDK Basic introduce.

快速教學如何使用php建構自己的APP

Facebook 中建立application 取得Keys
Facebook developer application page.

進入頁面後如下圖所表示,根據提示快速建立Application.
[![](http://1.bp.blogspot.com/_vekjcQZ8YNQ/TT_Cj_YE4FI/AAAAAAAAKtY/cqslh415XaA/s640/1.jpg)](http://1.bp.blogspot.com/_vekjcQZ8YNQ/TT_Cj_YE4FI/AAAAAAAAKtY/cqslh415XaA/s1600/1.jpg)
建立好app,可以進入到內部將Domain name, email, …等資料補齊。
**TIP:Domain name一定要設定好,不然會導致無法使用。**
[![](http://4.bp.blogspot.com/_vekjcQZ8YNQ/TT_DXB8bdSI/AAAAAAAAKtg/W_cxByM0MHo/s1600/3.jpg)](http://4.bp.blogspot.com/_vekjcQZ8YNQ/TT_DXB8bdSI/AAAAAAAAKtg/W_cxByM0MHo/s1600/3.jpg) []() [下載Facebook API for php](https://github.com/facebook/php-sdk/),把資料解壓縮開來,我們只需要兩個檔案。 **1. facebook.php** **2. example.php** 將兩個檔案放到同個資料夾當中,**修改example.php** `$facebook = new Facebook(array(

'appId' => '你的APP ID',

'secret' => '你的應用程式密鑰',

'cookie' => true,

));

` 接著檔案上傳到Server上,執行你的APP。 以下為簡化後的example.php內容。 `
require 'facebook.php';



// Create our Application instance (replace this with your appId and secret).

$facebook = new Facebook(array(

'appId' => 'appid',

'secret' => 'secret key',

'cookie' => true,

));



$session = $facebook->getSession();



$me = null;

// Session based API call.

if ($session) {

try {

$uid = $facebook->getUser();

$me = $facebook->api('/me');

} catch (FacebookApiException $e) {

error_log($e);

}

}



if ($me) {

$logoutUrl = $facebook->getLogoutUrl();

} else {

$loginUrl = $facebook->getLoginUrl();

}

?>







Clonn.info - Facebook API demo PHP SDK







Pure PHP SDK, without javascript











Session





undefined



You









Your User Object



undefined



You are not Connected.









` 直接想要看結果,可以**[點此連到範例頁面](http://clonn.info/demo/facebook/example.php),**頁面內容如下。
[![](http://4.bp.blogspot.com/_vekjcQZ8YNQ/TT_OzVzH4wI/AAAAAAAAKtk/uH-YM2a3Ipc/s1600/4.jpg)](http://4.bp.blogspot.com/_vekjcQZ8YNQ/TT_OzVzH4wI/AAAAAAAAKtk/uH-YM2a3Ipc/s1600/4.jpg)
[![](http://1.bp.blogspot.com/_vekjcQZ8YNQ/TT_PE5A0VNI/AAAAAAAAKto/fZgjA_5BrLg/s1600/6.jpg)](http://1.bp.blogspot.com/_vekjcQZ8YNQ/TT_PE5A0VNI/AAAAAAAAKto/fZgjA_5BrLg/s1600/6.jpg)
[![](http://4.bp.blogspot.com/_vekjcQZ8YNQ/TT_PVewt-ZI/AAAAAAAAKts/NfggqNlUVr8/s400/7.jpg)](http://4.bp.blogspot.com/_vekjcQZ8YNQ/TT_PVewt-ZI/AAAAAAAAKts/NfggqNlUVr8/s1600/7.jpg)

CaesarChi

Web developer, focus on website fullstack, special JavaScript, and love sharing developing experience and communicate with developers. http://about.me/clonn