本文介绍了Facebook PHP SDK/Graph API-返回我的名称/信息,但没有其他人吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Graph API和Facebook的PHP SDK尝试返回当前用户名,但是我很幸运能显示我的名字.这很简单,知道Graph API的人应该没有任何问题,仅通过查看正在使用的代码即可帮助我.

I'm trying to use the Graph API and the PHP SDK for Facebook to try and return the current users name, but I'm only having luck with it displaying my name. This is pretty straight forward and someone that knows the Graph API shouldn't have any problem helping me out just by looking at the code I'm using.

require 'facebook.php';

$appid=get_post_meta($thisID,'_EFBPDAppID',true);
$appsec=get_post_meta($thisID,'_EFBPDAppSec',true);
$facebook = new Facebook(array(
  'appId'  => $appid,
  'secret' => $appsec,
  'cookie' => true
));
$user = $facebook->getUser();
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;
  }
}

然后我用这个:echo $user_profile['name'];显示用户名,但只显示我的名字.在任何其他配置文件中,它仅显示该变量在何处为空白.在我的个人资料上,它显示了我的名字.我还使用了print_r($user);,它显示了我的ID,但是对于其他任何配置文件,它显示了0.

And then I use this: echo $user_profile['name']; to display the users name, but it is only displaying my name. On any other profile it just shows where that variable is as blank. On my profile, it shows my name. I also used print_r($user); and it is displaying my ID, but for any other profile it displays 0.

用户是否需要对自己进行身份验证,或者应该已经有一个使用此信息的会话?如果有会话信息,我该如何获取?

Do users need to authenticate themselves or should there already be a session with this info? How do I get that session info if there is one?

任何能帮助我的人都会非常感激.谢谢.

Anyone who can help out I would be very grateful. Thanks.

推荐答案

为了安全和隐私,Facebook用户还必须登录并允许当前应用程序访问其帐户信息.您可以使用其他SDK完成此操作.大多数人都使用Javascript,但我本人更喜欢PHP. HTML的简单实现如下所示:

For security and privacy as well, Facebook users must login and allow the current application to have access to their account information. You can accomplish this using different SDK. Most people use Javascript but I persoally prefer PHP. As simple implementation in HTML is shown below:

<a href="<?php echo $this->facebook->getLoginUrl(array('next' => "http://www.yourdomain.com/facebooklogin_success", 'req_perms' => 'email,read_stream,publish_stream,offline_access'))?>">Login to Facebook</a>

它将生成类似以下内容的

It will generate something like:

<a href="https://www.facebook.com/login.php?api_key=1234567890&cancel_url=http%3A%2F%2Fyourdomain.com/facebooklogin_cancel.php%2F&display=page&fbconnect=1&next=http%3A%2F%2Fyourdomain.com/facebooklogin_success.php&return_session=1&session_version=3&v=1.0&req_perms=email%2Cread_stream%2Cpublish_stream%2Coffline_access">Login to Facebook</a>

在该过程之后,您应该能够使用Graph API访问用户的信息.

you should be able to access the user's information after that process using Graph API.

$fbuser = $facebook->api('/me');
echo "Hello ".$fbuser['name'];

让我知道是否对您有用.

Let me know if works for you.

这篇关于Facebook PHP SDK/Graph API-返回我的名称/信息,但没有其他人吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 18:22