2008-12-20 39 views
9

我正在尋找一個好的,簡單的PHP函數來獲得我最新的Facebook狀態更新。任何人都知道嗎?PHP函數獲取Facebook狀態?

謝謝!

編輯:我添加下面半的解決方案。

或者如果有誰知道RSS feed中的閱讀和吐出最近狀態更新的好方法?

+0

有趣的問題。我也想要那個答案! – Skuta 2008-12-20 22:17:04

+0

可悲的是,幾乎所有下面描述的這些方法都過時了:/ – Petrogad 2011-02-21 21:58:47

回答

1

我似乎從來沒有與PEAR相處,但如果你有更好的運氣比我,那麼PEAR的解決方案似乎是最好的途徑長遠。

另一個想法是探索Facebook Developer API庫,看看是否有可能給你什麼你正在尋找。

最後,曾經有一種方式來獲得一個RSS feed ...但我似乎無法找到工作了指令,但是如果你感興趣,你可能會圍繞Facebook的幫助捅。礦最終看起來是這樣的:

http://www.new.facebook.com/feeds/status.php?id=[idnumber]&viewer=[viewer]&key=[key]&format=rss20

3

這是一個不完整的答案,但是這是我到目前爲止得到:

第一:add the developer application on FB。然後創建一個新應用程序。隨心所欲地調用它。

二:Download the PHP client.轉儲它的地方,在你的虛擬主機提供商,即/ Facebook的/

三:複製下面的初學者代碼來讓自己開始進入一個PHP文件:

<?php 
require_once('facebook/php/facebook.php'); 
$facebook = new Facebook("YOUR_API_KEY","YOUR_SECRET_KEY"); 
$result = $facebook->api_client->fql_query("SELECT status FROM user WHERE uid = YOURIDNUMBER"); 
// OR --- they both get the same data 
$result = $facebook->api_client->users_getInfo(YOURIDNUMBER,'status'); 
print_r($result); 
echo "<pre>Debug:" . print_r($facebook,true) . "</pre>"; // debug info 
?> 

其他信息:

我希望這可以幫助別人開始!

編輯:即使offline_access處於打開狀態,除非您是該人或他們的朋友(取決於他們的隱私設置),FB似乎不會讓您訪問某人的狀態。

,但我卻終於設法找到了新的配置文件版本的RSS提要:http://www.new.facebook.com/minifeed.php?filter=11

+0

您應該在某處看到某些東西。當你的php.ini文件中有display_errors = off時,空白輸出通常是一個致命錯誤的標誌,轉到apache error_log。 FB客戶端很好地運行PHP5,並通常在發生錯誤時拋出相當有意義和詳細的異常。 – Ian 2008-12-21 09:21:46

+0

我最終重置了我的SECRET密鑰並解決了問題。 但是,我的fql查詢不會給出任何結果,除非我包括:$ fb_user = $ facebook-> require_login();. offline_access權限似乎沒有正常工作,或者我正在做這個錯誤。 – 2008-12-21 09:37:29

0
<?php 
// see http://github.com/facebook/php-sdk/blob/master/facebook.php 
require './facebook.php'; 
// Create our Application instance. 
// see http://www.youtube.com/watch?v=jYqx-RtmkeU for how to get these numbers 
$facebook = new Facebook(array('appId' => 'XXX','secret' => 'XXX')); 
// This call will always work since we are fetching public data. 
// this could be /username or /username/friends etc... 
// see developer api for FQL for examples 
$status = $facebook->api('/haanmc/feed?limit=1'); 
?> 
<p><?php print $status['data'][0]['message']; ?></p> 
<p>Likes: <?php print $status['data'][0]['likes']; ?> | Comments: <?php print count($status['data'][0]['comments']['data']); ?></p> 
<textarea style="width: 95%; height: 600px;"><?php print_r($status); ?></textarea> 
2

我找到了一種獲取您最新的Facebook狀態的方法。這是你怎麼做的:

1)Create a facebook app,並複製你的應用程序的祕密和應用程序ID。

2)將應用程序read_stream和offline_access授予您的配置文件。 (http://developers.facebook.com/docs/authentication/permissions)要獲取您的最新狀態,應用程序需要一個access_token。通過授予offline_access,access_token應該「永不」過期。要做到這一點,最簡單的方法是單擊該代碼生成的按鈕:(請務必填寫「您的應用程序ID」和設置cookie來真的!)

<fb:login-button perms="read_stream,offline_access"></fb:login-button> 
<div id="fb-root"></div> 
<script src="http://connect.facebook.net/en_US/all.js"></script> 
<script>FB.init({appId: 'your app id', status: true, cookie: true, xfbml: true});</script> 

3)現在試圖找出的access_token它正在使用。 access_token保存在fbs_appId cookie中。找到它使用您的瀏覽器或使用$_COOKIE['fbs_appId']。尋找access_token=...

4)現在你有一個(希望)永遠不會過期的access_token您可以使用下面的代碼:

$access_token='xxxxxxxxxxxxxxxxxxxx'; 
$appId='123456789132456789'; 
$appSecret='xxxxxxxxxxxxxxxxxxxx'; 
$profileId='123456789'; 

//http://github.com/facebook/php-sdk/blob/master/src/facebook.php 
require 'facebook.php'; 

$facebook = new Facebook(array('appId' => $appId,'secret' => $appSecret)); 
$response = $facebook->api('/'.$profileId.'/feed?limit=1&access_token='.$access_token); 

5)信息顯示部分應位於:$response['data'][0]['message']

我不知道訪問令牌有效的時間長短。 Facebook說:

使您的應用程序可以隨時代表用戶執行授權請求。默認情況下,大多數訪問令牌在短時間後過期,以確保應用程序僅在主動使用應用程序時代表用戶發出請求。此權限使我們的OAuth端點返回的訪問令牌長久存在。

1

我知道它使用Jens的帖子來檢索有效的access_token。然後,我使用以下代碼從xml文件中提取狀態消息和發佈時間(可以更改$ limit以顯示更多或更少的狀態消息,或使用表單來更改它)。

請務必在您的Facebook ID和您創建的應用程序中獲取訪問令牌(請參閱Jens' post)。你可以檢查這個腳本的輸出here

玩得開心!

<?php 
if(isset($_POST['limit'])) { 
    $limit = $_POST['limit']; 
} 
else { 
    $limit = 3; // number of status messages to display 
} 
$f = fopen ("https://api.facebook.com/method/status.get?uid=YOUR_FACEBOOK_ID&limit=".$limit."&access_token=YOUR_ACCESS_TOKEN", "r"); 

while ($line= htmlentities(fgets($f))) { 
    if ($line===FALSE) print ("FALSE\n"); 
    else 
    { 
     $content = $content." ".$line; 
    } 
} 
fclose ($f); 

$message = explode("&lt;message&gt;", $content); // search for the <message> tag 
$message_cnt = count($message); 
$msg_index = 0; 

$time = explode("&lt;time&gt;", $content); // search for the <time> tag 

for($i=1; $i<$message_cnt; $i++) 
{ 
    $tmp = explode("&lt;/message&gt", $message[$i]); 
    $msg[$msg_index] = $tmp[0]; // status message 

    $tmp2 = explode("&lt;/time&gt", $time[$i]); 
    $t[$msg_index++] = $tmp2[0]; // time of posting 
} 

for($i=0; $i<$msg_index; $i++) 
{ 
    echo("<span class=\"status\">".preg_replace('!\015\012|\015|\012!','<br>',$msg[$i])."</span><br>\n 
      <span class=\"date\">on ".date("d.m.Y", $t[$i])." at ".date("H:i",$t[$i])."</span><br><br>\n"); 

} 
?> 
1

只需使用最快的方法PHPforFB框架(www.phpforfb.com/en/)。

的代碼看起來是這樣的:

require_once('phpforfb_framework.php'); 

$structInit = array('app_id' => APP_ID,'app_name' => APP_NAME,'sec_key' => APP_SECKEY); 

$FacebookAPP = new PHPforFB($structInit); 

if($FacebookAPP->lastErrorCode>0){ 
    //Creation failed => Display error message and exit 
    echo "PHPforFB Error: ".$FacebookAPP->lastErrorCode." -> ".$FacebookAPP->lastError; 
}else{ 
    //PHPforFB framework established 

    if($FacebookAPP->userLoggedIn === TRUE){ 

     //If the user is logged in at Facebook: 
     //Here you can determine if the user has at least once before 
     //granted basic permissions to your application. 
     if($FacebookAPP->userAuthenticated === FALSE){ 

      //The user has not yet granted permissions 
      //**your code here** 
     }else{ 
      //The user has already granted permissions, therefore his Facebook ID 
      //is known to us. It is always available in $FacebookAPP->userID: 

      $userID = $FacebookAPP->userID; 
      //**your code here** 
     } 
    } 
}