Facebook应用程序帮助初学者的基础知识和访问令牌PHP SDK

时间:2013-07-27 22:40:27

标签: facebook facebook-graph-api facebook-php-sdk facebook-access-token

我想知道如何轻松获取Facebook应用程序的访问令牌?我是新手,所以一步一步的解释将不胜感激。我打算制作一个显示用户数据的应用程序,如名称,生物,dob和电子邮件。此外,该应用程序还显示页面的提要。使用页面的ID /名称或类别。到目前为止,我有这个。当我试图尝试时,某些事情可能会更多。

//Loading the SDK
require_once("facebook.php");

//Setting up APP parameters
$config = array();
$config['appId'] = 'xxxxxxxxxxxx';
$config['secret'] = 'xxxxxxxxxxxx';

$facebook = new Facebook($config);
$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
// Getting the user id
$user = $facebook->getUser();
$user_me = $facebook->api('/me','GET');
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {

$params = array(
  'scope' => 'read_stream, user_about_me',
  'redirect_uri' => 'http://fblabs.chimpitup.com/'
);
  $loginUrl = $facebook->getLoginUrl($params);
}

1 个答案:

答案 0 :(得分:4)

如果您使用的是PHP SDK,则无需明确知道访问令牌。您也无需使用getAccessTokensetAccessToken来获取或设置它。所有这些都是由用户点击从$facebook->getLoginUrl($params);生成的链接,然后是函数getUser来处理的。

删除

$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);

以下行也会触发错误,因为它不在try-catch中,并且在接下来的几行中是多余的。

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

知道您已通过此验证

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

然后您可以使用条件

进行调用
if ($user) {
    $page_posts = $facebook->api('/PAGE_ID/posts');
}

PAGE_ID是您希望从

获取Feed的网页的ID

$page_posts是一组邮件对象,可以通过循环访问

来访问
foreach($page_posts['data'] as $post){
  $post_link = $post['actions'][0]['link'];
  $page_id = $post['from']['id'];
  $page_name = $post['from']['name'];
  $message = ($post['message']) ? $post['message'] : " ";
  $name = ($post['name']) ? $post['name'] : " ";
  $story = ($post['story']) ? $post['story'] : " ";
  $post_time = $post['updated_time'];
}

例如以HTML格式显示

    <div id="stream">
<?php foreach($user_posts['data'] as $post){  
  $post_link = $post['actions'][0]['link'];
  $page_id = $post['from']['id'];
  $page_name = $post['from']['name'];
  $message = ($post['message']) ? $post['message'] : " ";
  $name = ($post['name']) ? $post['name'] : " ";
  $story = ($post['story']) ? $post['story'] : " ";
  $post_time = $post['updated_time'];
?>
      <div class="post">    
        <div class="picture">
          <a href="http://facebook.com/<?php echo $page_id; ?>"><img src="http://graph.facebook.com/<?php echo $page_id; ?>/picture?type=square"/></a>
        </div>
        <div class="body">
          <a href="http://facebook.com/<?php echo $page_id; ?>" class="actor"><?php echo $page_name; ?></a>
          <span class="message"><?php echo $message; ?></span><br>
          <span class="message"><?php echo $name; ?></span><br>
          <span class="message"><?php echo $story; ?></span>
          <div class="meta">
            <a href="<?php echo $post_link ?>" class="permalink"><?php echo date("F j g:i a", strtotime($post_time)); ?></a>
          </div>
        </div>
      </div>
<?php } ?>
    </div><!-- end #stream -->

有关如何撰写网页帖子的示例,请参阅https://developers.facebook.com/tools/explorer/?method=GET&path=facebook%2Fposts,了解帖子对象的制作方式https://developers.facebook.com/docs/reference/api/post/

请参阅示例https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php和PHP入门部分https://developers.facebook.com/docs/php/gettingstarted/以及此答案中的部分代码来源https://github.com/phwd/hellopageposts/blob/master/index.php