Facebook应用程序的基本帮助和访问令牌PHP SDK为初学者



我想知道如何为我的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);
}

如果您使用的是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是您希望从获得提要的页面的id

$page_posts是可以通过循环访问的post对象的集合

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%2Posts获取页面帖子的组成示例,以及https://developers.facebook.com/docs/reference/api/post/了解如何使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

最新更新