PHP 函数获取 Facebook 状态?

发布于 2024-07-10 06:24:42 字数 163 浏览 21 评论 0 原文

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

谢谢!

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

或者是否有人知道读取 RSS 提要并吐出最近状态更新的好方法?

I'm looking for a good, simple PHP function to get my latest Facebook status updates. Anyone know of one?

Thanks!

EDIT: I've added a half-solution below.

Or if anyone knows a good way to read in the RSS feed and spit out the recent status update?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

冰之心 2024-07-17 06:24:42

快速检查 PEAR 发现 Services_Facebook

A quick check on PEAR found Services_Facebook

旧城空念 2024-07-17 06:24:42

这是一个不完整的答案,但这是我到目前为止得到的:

首先: 在 FB 上添加开发人员应用程序。 然后创建一个新的应用程序。 随便你怎么称呼它。

第二:下载 PHP 客户端。 转储它位于您的网站主机上的某个位置,即 /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
    权限并拥有
    添加了一个应用程序。
  • 您可以通过键入以下内容添加offline_access
    在以下网址中:
    http://www.facebook.com/authorize .php?api_key=YOUR_API_KEY&v=1.0&ext_perm=offline_access
  • 有关权限的更多信息可在此处找到:http://wiki.developers.facebook.com/index.php/Extended_permissions
  • 我正处于一个停止点:我的任何事情
    程序调用 fql 查询或
    users_getInfo,我的页面停止
    执行php? 我正在猜测
    通话数量有限
    对于新的应用程序? 我从来没有
    做过任何 FB 开发,所以我
    对它完全陌生。 也许使
    拨打电话并保存您最近的状态
    (或最近的状态)在您的
    拥有自己的数据库以防止过度调用
    API?

我希望这可以帮助某人入门!

编辑:似乎FB不会让你访问某人的状态,即使offline_access打开,除非你是那个人或他们的朋友(取决于他们的隐私设置)。

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

This is an incomplete answer, but this is what I've gotten so far:

First: add the developer application on FB. Then create a new application. Call it whatever you want.

Second: Download the PHP client. Dump it somewhere on your webhost, i.e. /facebook/

Third: Copy the following beginner code to get yourself started into a php file:

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

Other info:

  • You must be logged in and have the
    application added. OR you give the
    application offline_access
    permissions and have the
    aapplication added.
  • You can add offline_access by typing
    in the following url:
    http://www.facebook.com/authorize.php?api_key=YOUR_API_KEY&v=1.0&ext_perm=offline_access
  • more info on permissions found here: http://wiki.developers.facebook.com/index.php/Extended_permissions
  • I'm at a stopping point: anything my
    program calls the fql query or
    users_getInfo, my page stops
    executing the php? I'm guessing
    there are a limited amount of calls
    for new applications? I've never
    done any FB development so I'm
    completely new to it. Maybe make
    the call and save your recent status
    (or most recent statuses) in your
    own DB to prevent excessive calls to
    the API?

I hope this helps someone get started!

EDIT: It seems that FB won't let you access someones status, even if the offline_access is on, unless you are that person or their friend (depending on their privacy settings).

I did however, finally manage to find the RSS feed in the new profile version: http://www.new.facebook.com/minifeed.php?filter=11

飘逸的'云 2024-07-17 06:24:42

我找到了一种方法来获取您最新的 Facebook 状态。 操作方法如下:

1) 创建一个 Facebook 应用,然后复制您的应用程序秘密和应用程序 ID。

2) 授予应用程序对您的个人资料的read_stream 和offline_access 权限。 (http://developers.facebook.com/docs/authentication/permissions)获取您的最新状态应用程序需要 access_token。 授予offline_access 后,access_token 应该“永不”过期。 最简单的方法是单击此代码生成的按钮:(请务必填写“您的应用程序 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>

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

我不知道访问令牌的有效期是多长时间。 脸书说:

使您的应用程序能够随时代表用户执行授权请求。 默认情况下,大多数访问令牌会在短时间内过期,以确保应用程序仅在用户主动使用应用程序时才代表用户发出请求。 此权限使我们的 OAuth 端点返回的访问令牌长期有效。

I have found a way to fetch your latest facebook status. This is how you do it:

1) Create a facebook app, and copy your application secret and application id.

2) Grant the app read_stream and offline_access to your profile. (http://developers.facebook.com/docs/authentication/permissions) To fetch your latest status the app needs an access_token. With offline_access granted the access_token should "never" expire. The easiest way to do this is to click the button generated by this code: (be sure to fill in 'your app id' and set cookie to 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>

3) Now try to find out what access_token it is using. The access_token is saved in the fbs_appId cookie. Locate it using your browser or using $_COOKIE['fbs_appId']. Look for access_token=....

4) Now that you have a (hopefully) never expiring access_token you can use the following code:

$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) The message part should be located: $response['data'][0]['message']

I don't know HOW long the access token is valid. Facebook says:

Enables your application to perform authorized requests on behalf of the user at any time. By default, most access tokens expire after a short time period to ensure applications only make requests on behalf of the user when the are actively using the application. This permission makes the access token returned by our OAuth endpoint long-lived.

触ぅ动初心 2024-07-17 06:24:42

如果您只想获取最新状态,这是一个非常简单的功能。 它不依赖于 Facebook SDK 或任何东西。 您只需要 CURL 和 JSON 支持。

获取的简单 PHP 函数脸书状态

Here is a REALLY simple function if you just want to get the latest status. It doesn't depend on the Facebook SDK or anything. You just need CURL and JSON support.

Simple PHP function to get facebook status

零時差 2024-07-17 06:24:42

我似乎从来没有与 PEAR 相处过,但如果你的运气比我好,那么 PEAR 解决方案似乎是长期的最佳途径。

另一个想法是探索 Facebook Developer API 库,看看它是否可以为您提供任何帮助寻找。

最后,曾经有一种获取 RSS 提要的方法...但我似乎找不到任何不再有效的说明,但如果您感兴趣,您可以浏览 Facebook 帮助。 我的最终看起来像这样:

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

I never seem to get along with PEAR, but if you have better luck than I, then the PEAR solution seems the best route long term.

Another idea is to explore the Facebook Developer API library and see if that might give you anything you are looking for.

Lastly, there used to be a way to get an RSS feed... but I can't seem to find any instructions that work anymore, but you might poke around Facebook help if that interests you. Mine ends up looking something like this:

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

黑寡妇 2024-07-17 06:24:42

我使用 Jens 的帖子检索有效的 access_token 使其正常工作。 然后,我使用以下代码从 xml 文件中提取状态消息和发布时间(您可以更改 $limit 以显示更多或更少的状态消息,或使用表单来更改它)。

请务必输入您的 Facebook ID 和从您创建的应用程序获得的访问令牌(请参阅 Jens 的帖子)。 您可以在此处检查此脚本的输出。

玩得开心!

<?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("<message>", $content); // search for the <message> tag
$message_cnt = count($message);
$msg_index = 0;

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

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

    $tmp2 = explode("</time>", $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");

}
?>

I got it working using Jens' post to retrieve a valid access_token. Then, I extracted the status messages and the time of posting from the xml file using the following code (you can change $limit to display more or less status messages, or use a form to change it).

Be sure to put in your Facebook ID and the access token you got from the app you created (see Jens' post). You can check the output of this script here.

Have fun!

<?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("<message>", $content); // search for the <message> tag
$message_cnt = count($message);
$msg_index = 0;

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

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

    $tmp2 = explode("</time>", $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");

}
?>
淡笑忘祈一世凡恋 2024-07-17 06:24:42

在过去的几天里,我尝试了很多教程,但没有一个起作用。 我认为这可能是由于 facebook 改变了他们的 api 要求。 这是我发现目前唯一有效的:

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

I have tried loads of tutorials over the last few days and none of them have worked. I think it may be due to facebook changing their api requirements. This is the only one I found that works at the moment:

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

缺⑴份安定 2024-07-17 06:24:42

只需使用 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**
        }
    }
}

Just use PHPforFB framework (www.phpforfb.com/en/) for the fastest way.

The code looks like this:

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**
        }
    }
}
你又不是我 2024-07-17 06:24:42

由于我无法使用 API 路由,因此我使用了以下位置的 RSS: http://www.new.facebook.com/minifeed.php?filter=11

并使用了以下 PHP 函数,称为 StatusPress,经过我自己的一些修改,用于解析我的 Facebook 状态的 RSS 提要。 效果很好!

Since I couldn't use the API route, I went with the RSS found at: http://www.new.facebook.com/minifeed.php?filter=11

And used the following PHP function, called StatusPress, with some of my own modifications, to parse the RSS feed for my Facebook status. Works great!

东走西顾 2024-07-17 06:24:42
<?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>
<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文