PHP 函数来获取 Facebook 状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/383777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:33:59  来源:igfitidea点击:

PHP function to get Facebook status?

phpfacebookstatusupdates

提问by Bryan Denny

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

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

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?

或者,如果有人知道阅读 RSS 提要并吐出最近状态更新的好方法吗?

采纳答案by Bryan Denny

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

由于我无法使用 API 路由,因此我使用了 RSS,网址为: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!

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

回答by TonyUser

A quick check on PEARfound Services_Facebook

快速检查PEAR发现Services_Facebook

回答by Bryan Denny

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.

首先:在 FB 上添加开发者应用程序。然后创建一个新的应用程序。随心所欲地称呼它。

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

第二:下载PHP客户端。将其转储到您的虚拟主机上的某个位置,即 /facebook/

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

第三:将以下初学者代码复制到一个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
 ?>

Other 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 的过度调用?

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

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

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

然而,我最终设法在新的个人资料版本中找到了 RSS 提要:http: //www.new.facebook.com/minifeed.php?filter=11

回答by Jens

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

我找到了一种方法来获取您最新的 Facebook 状态。这是你如何做到的:

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

1)创建一个 facebook 应用程序,并复制您的应用程序机密和应用程序 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!)

2) 授予应用 read_stream 和 offline_access 访问您的个人资料。( http://developers.facebook.com/docs/authentication/permissions) 要获取您的最新状态,该应用程序需要一个 access_token。授予 offline_access 后, access_token 应该“永不”过期。最简单的方法是点击这段代码生成的按钮:(一定要填写'your app 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) 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=....

3) 现在尝试找出它使用的是什么 access_token。access_token 保存在 fbs_appId cookie 中。使用浏览器或使用$_COOKIE['fbs_appId']. 寻找access_token=....

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

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

5)消息部分应位于: $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.

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

回答by Lenny

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.

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

Simple PHP function to get facebook status

获取 Facebook 状态的简单 PHP 函数

回答by FunnelWebDave

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:

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

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

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

回答by penko

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

只需使用 PHPforFB 框架 ( www.phpforfb.com/en/) 即可获得最快的方式。

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

回答by MichL

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

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

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.

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

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("&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('!52|5|2!','<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");

}
?>

回答by jasonrm

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.

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

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

另一个想法是探索Facebook Developer API库,看看它是否可以为您提供您正在寻找的任何东西。

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:

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

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

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

回答by joshmiller

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