代码之家  ›  专栏  ›  技术社区  ›  Bryan Denny

获取Facebook状态的PHP函数?

  •  9
  • Bryan Denny  · 技术社区  · 17 年前

    我正在寻找一个好的,简单的PHP函数来获取我最新的Facebook状态更新。有人知道吗?

    编辑: 我在下面添加了半个解决方案。

    10 回复  |  直到 17 年前
        1
  •  6
  •   TonyUser    17 年前
        2
  •  3
  •   Bryan Denny    17 年前

    第一: 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
     ?>
    

    其他资料:

    我希望这有助于某人开始!

    似乎FB不允许你访问某人的状态,即使离线访问已打开,除非你是此人或他们的朋友(取决于他们的隐私设置)。

    但是,我最终在新的配置文件版本中找到了RSS提要: http://www.new.facebook.com/minifeed.php?filter=11

        3
  •  2
  •   Jens    16 年前

    1) Create a facebook app ,并复制您的应用程序机密和应用程序id。

    http://developers.facebook.com/docs/authentication/permissions )要获取您的最新状态,应用程序需要访问令牌。授予脱机\u访问权限后,访问\u令牌应“永不”过期。最简单的方法是单击此代码生成的按钮:(确保填写“您的应用程序id”并将cookie设置为true!)

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

    $_COOKIE['fbs_appId'] . 寻找 access_token=... .

    4) 现在您有了一个(希望)永不过期的访问令牌,您可以使用以下代码:

    $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说:

        5
  •  1
  •   jasonrm    17 年前

    我似乎从来没有和梨子相处过,但如果你的运气比我好,那么从长远来看,梨子解决方案似乎是最好的办法。

    另一个想法是探索 Facebook Developer API

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

        6
  •  1
  •   Community Mohan Dere    9 年前

    我使用Jens的帖子来检索有效的访问令牌。然后,我使用以下代码从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");
    
    }
    ?>
    
        7
  •  1
  •   FunnelWebDave    15 年前

    在过去的几天里,我尝试了大量的教程,但没有一本有效。我认为这可能是因为facebook改变了他们的api要求。这是目前我发现唯一有效的方法:

    http://www.deanblog.co.uk/article/13/adding-a-facebook-status-feed-to-your-website-with-php

        8
  •  1
  •   penko    14 年前

    只需使用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**
            }
        }
    }
    
        9
  •  0
  •   Bryan Denny    17 年前

    http://www.new.facebook.com/minifeed.php?filter=11

    并使用以下PHP函数, called StatusPress ,并进行了一些修改,以解析我的Facebook状态的RSS提要。很好!

        10
  •  0
  •   joshmiller    16 年前
    <?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>