PageRenderTime 27ms CodeModel.GetById 19ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/examples/with_js_sdk.php

http://github.com/facebook/php-sdk
PHP | 59 lines | 52 code | 5 blank | 2 comment | 3 complexity | 555e738cb8c6b42834a5c5a2083bd3e6 MD5 | raw file
 1<?php
 2
 3require '../src/facebook.php';
 4
 5$facebook = new Facebook(array(
 6  'appId'  => '344617158898614',
 7  'secret' => '6dc8ac871858b34798bc2488200e503d',
 8));
 9
10// See if there is a user from a cookie
11$user = $facebook->getUser();
12
13if ($user) {
14  try {
15    // Proceed knowing you have a logged in user who's authenticated.
16    $user_profile = $facebook->api('/me');
17  } catch (FacebookApiException $e) {
18    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
19    $user = null;
20  }
21}
22
23?>
24<!DOCTYPE html>
25<html xmlns:fb="http://www.facebook.com/2008/fbml">
26  <body>
27    <?php if ($user) { ?>
28      Your user profile is
29      <pre>
30        <?php print htmlspecialchars(print_r($user_profile, true)) ?>
31      </pre>
32    <?php } else { ?>
33      <fb:login-button></fb:login-button>
34    <?php } ?>
35    <div id="fb-root"></div>
36    <script>
37      window.fbAsyncInit = function() {
38        FB.init({
39          appId: '<?php echo $facebook->getAppID() ?>',
40          cookie: true,
41          xfbml: true,
42          oauth: true
43        });
44        FB.Event.subscribe('auth.login', function(response) {
45          window.location.reload();
46        });
47        FB.Event.subscribe('auth.logout', function(response) {
48          window.location.reload();
49        });
50      };
51      (function() {
52        var e = document.createElement('script'); e.async = true;
53        e.src = document.location.protocol +
54          '//connect.facebook.net/en_US/all.js';
55        document.getElementById('fb-root').appendChild(e);
56      }());
57    </script>
58  </body>
59</html>