2011-08-25 11 views
12

का उपयोग कर अनुमति मांगना मैं नए PHP एसडीके का उपयोग करके अनुमतियों के लिए कैसे पूछ सकता हूं? मैं ग्राफ एपीआई का उपयोग नहीं करना चाहता हूं और यूआरएल को हर समय पार्स नहीं करना चाहता हूं। जब एप्लिकेशन खोला जाता है तो उसे स्वचालित रूप से अनुमतियों के लिए पूछना चाहिए यदि उपयोगकर्ता ने पहले से ही एक नहीं दिया है।नए PHP एसडीके (3.X.X)

+0

इसका क्या मतलब है? आप '$ facebook-> getLoginUrl()' हर समय उपयोग नहीं करना चाहते हैं? – ifaour

उत्तर

20

यहाँ कैसे मैं पीएचपी-एसडीके 3.2.0 के लिए नवीनतम पीएचपी एसडीके (3.0.1) ACCESS_TOKEN साथ गुंजाइश और लॉगआउट साथ

// init new facebook class instance with app info (taken from the DB) 
$facebook = new Facebook(array(
    'appId' => 'YOUR APP ID', 
    'secret' => 'YOUR APP SECRET' 
)); 
// get user UID 
$fb_user_id = $facebook->getUser(); 

    // get the url where to redirect the user 
$location = "". $facebook->getLoginUrl(array('scope' => 'publish_stream, email')); 

// check if we have valid user 
if ($fb_user_id) { 
    try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $fb_user_profile = $facebook->api('/me'); 

    } catch (FacebookApiException $e) { 
     $fb_user_id = NULL; 
     // seems we don't have enough permissions 
     // we use javascript to redirect user instead of header() due to Facebook bug 
     print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>'; 

     // kill the code so nothing else will happen before user gives us permissions 
     die(); 
    } 

} else { 
    // seems our user hasn't logged in, redirect him to a FB login page 

    print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>'; 

    // kill the code so nothing else will happen before user gives us permissions 
    die(); 
} 

// at this point we have an logged in user who has given permissions to our APP 
// basic user info can be fetched easily 
print "Welcome to my app". $fb_user_profile['name']; 
+0

'कुकी' पैरामीटर का उपयोग नवीनतम PHP एसडीके के साथ नहीं किया जाता है। आप इसे हटा सकते हैं। – Phillip

+1

धन्यवाद, मुझे लगता है कि यह PHP एसडीके के पुराने संस्करण से वहां छोड़ा गया था –

2

सत्र आधारित लॉग इन के साथ यह कर रहा हूँ है।

<?php 
require './src/facebook.php'; 
$facebook = new Facebook(array(
    'appId' => '135669679827333', 
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxx', 
)); 
$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) { 
    $user = null; 
    } 
} 
if ($user) { 
    $params = array(access_token => ''.$access_token.''); 
    $logoutUrl = $facebook->getLogoutUrl($params); 
} else { 
    $params = array(
    scope => 'read_stream,publish_stream,publish_actions,read_friendlists', 
    //redirect_uri => $url 
); 
    $loginUrl = $facebook->getLoginUrl($params); 
}; 
$access_token = $_SESSION['fb_135669679827333_access_token']; 
?> 

<?php if($_SESSION['fb_135669679827333_access_token']): ?> 
    <a href="<?php echo $logoutUrl; ?>&access_token=<?php echo $access_token; ?>" target="_parent">Login & Connect</a> 
<?php else: ?> 
    <a href="<?php echo $loginUrl; ?>" target="_parent">Login & Connect</a> 
<?php endif ?> 
संबंधित मुद्दे