2011-08-04 58 views
2

我正在嘗試製作Facebook應用,並且需要用戶在Facebook上打開應用程序時的用戶標識。我已經在Facebook畫布上設置了我的應用程序和它的模擬表單,我需要幫助將Facebook API連接到我的頁面,我需要爲此下載一個API嗎?我如何從Facebook發送給應用程序的JSON對象獲取用戶標識?從PHP獲取用戶標識在PHP中

我測試的applcation是:

<?php 

echo "this is working"; 

?> 

這是工作在Facebook上。

回答

8

下載PHP SDK

一個非常簡單的代碼示例,以獲取用戶ID - 如果用戶登錄和已授權的應用程序,然後$facebook->getUser()會給你的用戶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 Developers Site

0

這是一種由我寫的黑客代碼,允許提取任何的Facebook用戶ID即使他沒有登錄或通過應用 https://github.com/invisiblevision/get-facebook-id/

<?php 
$profile_url = 'https://facebook.com/profileUrl'; 
function get_web_page($url) 
    { 
     $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0'; 
     $options = array(
      CURLOPT_CUSTOMREQUEST =>"GET",  //set request type post or get 
      CURLOPT_POST   =>false,  //set to GET 
      CURLOPT_USERAGENT  => $user_agent, //set user agent 
      CURLOPT_COOKIEFILE  =>"cookie.txt", //set cookie file 
      CURLOPT_COOKIEJAR  =>"cookie.txt", //set cookie jar 
      CURLOPT_RETURNTRANSFER => true,  // return web page 
      CURLOPT_HEADER   => false, // don't return headers 
      CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
      CURLOPT_ENCODING  => "",  // handle all encodings 
      CURLOPT_AUTOREFERER => true,  // set referer on redirect 
      CURLOPT_CONNECTTIMEOUT => 120,  // timeout on connect 
      CURLOPT_TIMEOUT  => 120,  // timeout on response 
      CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 
     ); 
     $ch  = curl_init($url); 
     curl_setopt_array($ch, $options); 
     $content = curl_exec($ch); 
     $err  = curl_errno($ch); 
     $errmsg = curl_error($ch); 
     $header = curl_getinfo($ch); 
     curl_close($ch); 
     $header['errno'] = $err; 
     $header['errmsg'] = $errmsg; 
     $header['content'] = $content; 
     return $header; 
    } 

/*Getting user id */ 
$url = 'http://findmyfbid.com'; 
$data = array('url' => $profile_url); 
// use key 'http' even if you send the request to https://... 
$options = array(
    'http' => array(
     'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
     'method' => 'POST', 
     'content' => http_build_query($data), 
    ), 
); 
$context = stream_context_create($options); 
$result = file_get_contents($url, false, $context); 
function getData($data) 
{ 
    $dom = new DOMDocument; 
    $dom -> loadHTML($data); 
    $divs = $dom -> getElementsByTagName('code'); 
    foreach ($divs as $div) 
    { 
      return $div -> nodeValue; 

    } 
} 
$uid = getData($result); // User ID 
授權