2010-04-15 39 views
0

我驗證:通的Facebook從PHP通過PHP連接會話的JavaScript

// the php facebook api downloaded at step 3 
require_once("facebook-client/facebook.php"); 

// start facebook api with the codes defined in step 1. 
$fb=new Facebook('XXXXXXXXXXXXXXXXXXXX' , 'a3eaa49141ed38e230240e1b6368254a'); 
$fb_user=$fb->get_loggedin_user(); 

var_dump($fb_user); 

和會話在PHP創建。我想從這裏開始使用它與facebook的JavaScript客戶端庫。我該怎麼做呢?

回答

0

有了這個,你可以使用Facebook->setSession()功能

<?php 

require 'facebook.php'; 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
'appId' => $appId, 
'secret' => $secret, 
'cookie' => true, 
)); 

$_SESSION['facebook'] = $facebook; 


// We may or may not have this data based on a $_GET or $_COOKIE based session. 
// 
// If we get a session here, it means we found a correctly signed session using 
// the Application Secret only Facebook and the Application know. We dont know 
// if it is still valid until we make an API call using the session. A session 
// can become invalid if it has already expired (should not be getting the 
// session back in this case) or if the user logged out of Facebook. 
$session = $facebook->getSession(); 

$me = null; 
// Session based API call. 
if ($session) { 
try { 
$uid = $facebook->getUser(); 
$me = $facebook->api('/me'); 
} catch (FacebookApiException $e) { 
//error_log($e); 
} 
}else{ 
    //pass in json object of FB._session and convert it to array 
    $session = json_decode(stripslashes($_POST['session'])); 
    $session = (array) $session; 


    if($session) 
    $facebook->setSession($session); 
} 
?> 

var constant = {}; 
constant.session = <?php echo json_encode($session); ?>; 
window.fbAsyncInit = function() { 
    FB.init({ 
     appId : '<?php echo $facebook->getAppId(); ?>', 
     session : constant.session, // don't refetch the session when PHP already has it 
     status : true, // check login status 
     cookie : true, // enable cookies to allow the server to access the session 
     xfbml : true // parse XFBML 
    }); 
    if(!FB._session) 
     stream.load(); 
    else 
     jwpage.getAlbums(); 
    // whenever the user logs in, we refresh the stream 
    FB.Event.subscribe('auth.login', function() { 
     jwpage.getAlbums(); 
    }); 
    }; 

    (function() { 
    var e = document.createElement('script'); 
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; 
    e.async = true; 
    document.getElementById('fb-root').appendChild(e); 
}()); 
</script> 
促進從PHP會話以JavaScript和從JavaScript到PHP