2011-08-14 7 views
0

여기서부터 어떻게해야합니까? 이것은 sdk에서 제공된 예제에서 거의 복사 복사입니다. 나는이 사람들이 어떻게이 API로 무엇을 만들 수 있는지 이해하지 못한다. 로그인 등의 프롬프트 화면을 열려면 어떻게해야합니까? 도대체 페이스 북이 그것에 대해 뭐라고 말합니까?Facebook API 도움말

<?php 

require 'fb_sdk/src/facebook.php'; 

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

// Get User ID 
$user = $facebook->getUser(); 

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

if ($user) { 
    try { 
    // Proceed knowing you have a logged in user who's authenticated. 
    $user_profile = $facebook->api('/me'); 
    } catch (FacebookApiException $e) { 
    error_log($e); 
    $user = null; 
    } 
} 

// Permissions requested from the user. 
$par = array(); 
$par['scope'] = 'user_about_me, read_friendlists'; 

// Login or logout url will be needed depending on current user state. 
if ($user) { 
    $logoutUrl = $facebook->getLogoutUrl(); 
} else { 
    $loginUrl = $facebook->getLoginUrl($par); 
} 

?> 

답변

1

그래프를 사용자 인증 here on Facebook Developers에 사용하는 방법에 대한 정보를 찾을 수 있습니다.

$loginUrl = $facebook->getLoginUrl($par);으로 변수 $loginUrl은 인증 대화 상자에 대한 URL을 포함합니다. 대부분의 개발자 중 하나를 링크로 사용자 또는 자바 스크립트로 리디렉션을 수행하는 현재의이 - 예 :

die('<script>top.location.href = "' . $loginUrl . '"</script>'); 

다른 대안은 쿠키가있는 경우 (인증하는 XFBML와 자바 스크립트 SDK를 사용하는 것입니다 모두 SDK를 함께 사용 가능 그들이) 세션 데이터를 공유 할 것 - 예를 here에서 :

<?php 

require 'php-sdk/src/facebook.php'; 

$facebook = new Facebook(array(
    'appId' => 'YOUR_APP_ID', 
    'secret' => 'YOUR_APP_SECRET', 
)); 

// See if there is a user from a cookie 
$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) { 
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
    $user = null; 
    } 
} 

?> 
<!DOCTYPE html> 
<html xmlns:fb="http://www.facebook.com/2008/fbml"> 
    <body> 
    <?php if ($user_profile) { ?> 
     Your user profile is 
     <pre>    
     <?php print htmlspecialchars(print_r($user_profile, true)) ?> 
     </pre> 
    <?php } else { ?> 
     <fb:login-button></fb:login-button> 
    <?php } ?> 
    <div id="fb-root"></div> 
    <script>    
     window.fbAsyncInit = function() { 
     FB.init({ 
      appId: '<?php echo $facebook->getAppID() ?>', 
      cookie: true, 
      xfbml: true, 
      oauth: true 
     }); 
     FB.Event.subscribe('auth.login', function(response) { 
      window.location.reload(); 
     }); 
     FB.Event.subscribe('auth.logout', function(response) { 
      window.location.reload(); 
     }); 
     }; 
     (function() { 
     var e = document.createElement('script'); e.async = true; 
     e.src = document.location.protocol + 
      '//connect.facebook.net/en_US/all.js'; 
     document.getElementById('fb-root').appendChild(e); 
     }()); 
    </script> 
    </body> 
</html> 

또한 자바 스크립트 순수하게 로그인 할 수 FB.Login를 사용하여 :

FB.login(function(response) { 
    if (response.authResponse) { 
    console.log('Welcome! Fetching your information.... '); 
    FB.api('/me', function(response) { 
     console.log('Good to see you, ' + response.name + '.'); 
     FB.logout(function(response) { 
     console.log('Logged out.'); 
     }); 
    }); 
    } else { 
    console.log('User cancelled login or did not fully authorize.'); 
    } 
}, {scope: 'user_about_me, read_friendlists'}); 
+0

G ood 응답, @ 졸리 그드 : API를 생각하지 마라. 앱을 가지고 * 당신을 도울 수 있습니다. 앱이 제공 할 내용을 분명히 알게되면 Facebook API와의 통합 지점이 명확 해집니다. 이 [sample] (http://developers.facebook.com/docs/samples/canvas/) 앱을 예로 들어 보면 앱 아이디어를 실현하고 Facebook의 사용자 이름, 이메일, 사진 및 친구들이 필요하다고 결론을 내릴 수 있습니다. 그에 따라 API를 사용하십시오. – ifaour

0

는 그것은 주석에 언급되어

// Login or logout url will be needed depending on current user state. 
if ($user) { 
    $logoutUrl = $facebook->getLogoutUrl(); 
} else { 
    $loginUrl = $facebook->getLoginUrl($par); 
} 

SDK는 당신이 그것에 대해 걱정하지 않아도, 그 처리됩니다. 사용자 세부 정보는 $ user_profile에 있습니다. 은 echo"<pre>"; print_r($user_profile); echo"</pre>";입니다.