简体   繁体   English

在PHP中获取Facebook用户个人资料数据

[英]Get Facebook user profile data in php

The following code should return the email address of the user logged in. 以下代码应返回已登录用户的电子邮件地址。

  <?php


     require_once('facebook-php-sdk/src/facebook.php');
    // Create our Application instance (replace this with your appId and secret).
    $facebook= new Facebook(array(
      'appId' => '2453463636',
          'secret' => '365653656565656',
          'allowSignedRequest' => false,
    ));


    $user = $facebook->getUser();
    print_r($user);
    if ($user) {
      try {
        // Get the user profile data you have permission to view
       // $user_profile = $facebook->api('/me');
        $user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
        echo $user_profile['email'];

      } catch (FacebookApiException $e) {
        $user = null;
 error_log($e);
     print_r($e);
      }
    } else {
     die("failed");
    }

    ?>

Instead it returns "failed", and print_r($user) returns 0. 相反,它返回“失败”,而print_r($ user)返回0。

Really don't know what to do. 真的不知道该怎么办。

In the dashboard: 在仪表板中:

在此处输入图片说明

I have set up a website platform to view my app. 我已经建立了一个网站平台来查看我的应用程序。

The jist: The "App Domains" = www.app.com www.app.com :“应用程序域” = www.app.com

"Site Url" = http://www.app.com/test “站点网址” = http://www.app.com/test

The site url is actual the app. 网站网址是实际的应用。 I try to access the getUser in urls such as http://www.app.com/test/fbuser , where it returns 0 and die clause. 我尝试访问诸如http://www.app.com/test/fbuser URL中的getUser ,它返回0和die子句。

Did I set this up wrong? 我把这个设置错了吗?

EDIT 编辑

Curl on my localhost server 在我的本地主机服务器上卷曲

在此处输入图片说明

also the "Configure Command" is quite different(see below for localhost screen shot) 另外,“配置命令”也大不相同(请参阅下面的localhost屏幕快照)

在此处输入图片说明

The remote server info mentions "curl" in this blob of text for Configure Command 远程服务器信息在“ Configure Command”的此文本框中提到“ curl”

Codeigniter deletes $_REQUEST which is used by the Facebook PHP SDK to retrieve the OAuth code param. Codeigniter删除$_REQUEST ,Facebook PHP SDK使用该$_REQUEST来检索OAuth代码参数。

The fix is to copy $_GET to $_REQUEST before your call to $facebook->getUser() : 解决方法是在调用$facebook->getUser()之前将$_GET复制到$_REQUEST

$_REQUEST += $_GET; 
$user = $facebook->getUser();

Thanks to this SO answer. 感谢这个答案。

Case 1 If User is logged in and still getting $user = 0 情况1如果用户已登录并且仍然获得$ user = 0

I faced this issue on different occasions but SDK issue is not happening to everyone. 我在不同场合遇到了这个问题,但并不是所有人都遇到了SDK问题。 So I'm not sure what goes wrong here. 所以我不确定这里出了什么问题。 But I dig in to it little bit and found solution as mentioned below. 但我对此进行了深入研究,并找到了如下所述的解决方案。

SOLUTION : This worked for me after trying for many solutions for this issue. 解决方案:在尝试了此问题的许多解决方案后,此方法对我有用。

In base_facebook.php file, find the makeRequest() method and check for following Line. 在base_facebook.php文件中,找到makeRequest()方法并检查以下行。

$opts = self::$CURL_OPTS; 

Immediately following it, add this line 紧随其后,添加此行

$opts[CURLOPT_SSL_VERIFYPEER] = false;  

More details can be found here - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/ 可以在此处找到更多详细信息-http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/

Case 2 - If user is not Logged in 情况2-如果用户未登录

Try following 尝试跟随

if($user_id) {
  // User is logged in do action here

} else {

  // No user, print a link for the user to login
  $login_url = $facebook->getLoginUrl();
  echo 'Please <a href="' . $login_url . '">login.</a>';

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM