在PHP中从Facebook获取用户标识 [英] Get User Id from Facebook in PHP

查看:138
本文介绍了在PHP中从Facebook获取用户标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户打开应用程序时,我正在尝试制作一个Facebook应用,并且需要Facebook上的用户ID。我已经在Facebook画布上设置了我的应用程序和它的显示模拟窗体,我需要帮助将Facebook API连接到我的页面,我需要下载一个API吗?我如何从用户名称发送给应用程序的JSON对象中获取用户ID?

I am trying to make a Facebook app and need user id from Facebook when the user opens the applicaiton. I have setup my application and its show mock form on the Facebook canvas, I need help in connecting Facebook API to my page, would i need to download a API for that? And how do I get the user id from the JSON object which facebook says it sends to the application?

我的测试应用是:

<?php

echo "this is working";

?> 

这是在Facebook上工作。

This is working on Facebook.

推荐答案

下载 PHP SDK

一个非常简单的代码示例来获取用户ID - 如果用户已经登录并且已经授权了应用程序,那么 $ facebook-> getUser() 将给你的用户ID:

A very simple code example to get the user id - if the user is logged in and has authorised the application then $facebook->getUser() will give you the users id:

require 'facebook.php';

$facebook = new Facebook(array(
  'appId'  => 'YOUR_APP_ID',
  'secret' => 'YOUR_APP_SECRET',
));

// Get User ID
$user = $facebook->getUser();

if ($user) {
  try {
    // Get the user profile data you have permission to view
    $user_profile = $facebook->api('/me');
    echo "<pre>";
    print_r($user_profile);
    echo "</pre>";
  } catch (FacebookApiException $e) {
    $user = null;
  }
} else {
  die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}

查看SDK中的示例和 Facebook开发者网站

Take a look a the examples within the SDK and on the Facebook Developers Site.

这篇关于在PHP中从Facebook获取用户标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆