Blame view

social/facebook/example.php 3.58 KB
42868d70   andryeyev   Создал GIT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  <?php
  
  require 'facebook.php';
  
  // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
    'appId'  => '199223776754925',
    'secret' => '7fc238a93158a031f31a09de35d723a9',
    'cookie' => true,
  ));
  
  // 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);
    }
  }
  
  // login or logout url will be needed depending on current user state.
  if ($me) {
    $logoutUrl = $facebook->getLogoutUrl();
  } else {
    $loginUrl = $facebook->getLoginUrl();
  }
  
  // This call will always work since we are fetching public data.
  $naitik = $facebook->api('/naitik');
  
  ?>
  <!doctype html>
  <html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
      <title>php-sdk</title>
      <style>
        body {
          font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
        }
        h1 a {
          text-decoration: none;
          color: #3b5998;
        }
        h1 a:hover {
          text-decoration: underline;
        }
      </style>
    </head>
    <body>
      <!--
        We use the JS SDK to provide a richer user experience. For more info,
        look here: http://github.com/facebook/connect-js
      -->
      <div id="fb-root"></div>
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId   : '<?php echo $facebook->getAppId(); ?>',
            session : <?php echo json_encode($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
          });
  
          // whenever the user logs in, we refresh the page
          FB.Event.subscribe('auth.login', function() {
            window.location.reload();
          });
        };
  
        (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>
  
  
      <h1><a href="example.php">php-sdk</a></h1>
  
      <?php if ($me): ?>
      <a href="<?php echo $logoutUrl; ?>">
        <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
      </a>
      <?php else: ?>
      <div>
        Using JavaScript &amp; XFBML: <fb:login-button></fb:login-button>
      </div>
      <div>
        Without using JavaScript &amp; XFBML:
        <a href="<?php echo $loginUrl; ?>">
          <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
        </a>
      </div>
      <?php endif ?>
  
      <h3>Session</h3>
      <?php if ($me): ?>
      <pre><?php print_r($session); ?></pre>
  
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $uid; ?>/picture">
      <?php echo $me['name']; ?>
  
      <h3>Your User Object</h3>
      <pre><?php print_r($me); ?></pre>
      <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
      <?php endif ?>
  
      <h3>Naitik</h3>
      <img src="https://graph.facebook.com/naitik/picture">
      <?php echo $naitik['name']; ?>
    </body>
  </html>