Get User Id from Facebook in PHP

前端 未结 2 509
耶瑟儿~
耶瑟儿~ 2020-12-29 17:19

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 can

2条回答
  •  太阳男子
    2020-12-29 17:55

    Download the PHP SDK

    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 "
    ";
        print_r($user_profile);
        echo "
    "; } catch (FacebookApiException $e) { $user = null; } } else { die(''); }

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

提交回复
热议问题