jc1arke
8/6/2010 - 6:33 AM

index.php

<?php
  include_once "fbmain.php";
  $config['baseurl'] = "http://codetest.co.za/week9/";
  
  // Check if user session is valid and user is signed in
  if( $fbMe ) :
    // Retrieve the movies that the user likes via the Graph API
    try
    {
      $movies = $facebook -> api( '/me/movies' );
    }
    catch( Exception $e )
    {
      d( $e );
    }
    
    // Legacy example : users.getinfo
    try
    {
      $param = array(
        'method' => 'users.getinfo',
        'uids' => $fbMe['id'],
        'fields' => 'name,current_location,profile_url',
        'callback' => ''
      );
      $userinfo = $facebook -> api( $param );
    }
    catch( Exception $e )
    {
      d( $e );
    }
    
    // Update the user's status via Graph API
    if( isset( $_POST['status_update'] ) ) :
      try
      {
        $status_update = $facebook -> api( '/me/feed', 'post', array( 'message' => $_POST['status_update'], 'cb' => '' ) );
      }
      catch( FacebookApiException $e )
      {
        d( $e );
      }
    endif;
  endif;
  
  // FQL Query example using Legacy method call
  try
  {
    // Get the User's ID
    $uid = $facebook -> getUser(); // You could also use $fbMe['id'];
    
    $fql = "select name, hometown_location, sex, pic_square from user where uid=" . $uid;
    $param = array( 
      'method' => 'fql.query',
      'query' => $fql,
      'callback' => ''
    );
    
    $fqlResult = $facebook -> api( $param );
  }
  catch( Exception $e )
  {
  }
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>FaceBook SDK & Graph API Tut via PHP | iMod</title>
</head>
<body>
  <div id="fb-root"></div>
  <script type="text/javascript">
    window.fbAsyncInit = function() {
      // Initialize the FaceBook connection via the Application ID
      FB.init( { appId: '<?php echo $fbconfig['appid']; ?>', status: true, cookie: true, xfbml: true } );
      // Register the FB events
      FB.Event.subscribe( 'auth.login', function( response ) {
        login();
      });
      FB.Event.subscribe( 'auth.logout', function( response ) {
        logout();
      });
    };
    (function() {
      var e = document.createElement('script');
      e.type = 'text/javascript';
      e.src = document.location.protocol + 
        '//connect.facebook.net/en_US/all.js';
      e.async = true;
      document.getElementById( 'fb-root' ).appendChild(e);
    });
    function login() {
      document.location.href = "<?php echo $config['baseurl']; ?>";
    }
    function logout() {
      document.location.href = "<?php echo $config['baseurl']; ?>";
    }
  </script>
  <style>
    .box {
      margin: 5px;
      border: 1px solid #60729b;
      padding: 5px;
      width: 500px;
      height: 200px;
      overflow: auto;
      background-color: #e6ebf8;
    }
  </style>
  <h3>FaceBook SDK & Graph API Tut via PHP | iMod</h3>
  <?php if( ! $fbMe ) : ?>
    You have to login via the FB Login Button to see the SDK in action ;)
  <?php endif; ?>
  <p>
    <fb:login-button autologoutlink="true" 
      perms="email,user_birthday,status_update,publish_stream">
    </fb:login-button>    
  </p>
  <!-- always remember to check if the user session is valid or not -->
  <?php if( $fbMe ) : ?>
  <table border="0" cellpadding="3" cellspacing="3">
    <tr>
      <td>
        <!-- Data retrieved from the user's profile is shown in this box -->
        <div class="box">
          <strong>User Information using Graph API</strong>
          <?php d($fbMe); ?>
        </div>
      </td>
      <td>
        <div class="box">
          <strong>User likes the following movies (via Graph API)</strong>
          <?php d($movies); ?>
        </div>
      </td>
    </tr>
    <tr>
      <td>
        <div class="box">
          <strong>User Information using Legacy Call "users.getinfo"</strong>
          <?php d($user_info); ?>
        </div>
      </td>
      <td>
        <div class="box">
          <strong>FQL Query Example using Legacy Call "fql.query"</strong>
          <?php d(fqlResult); ?>
        </div>
      </td>
    </tr>
  </table>
  <div class="box">
    <form name="" action="<?php echo $config['baseurl']; ?>" method="post">
      <label for="status_update">Staus Update via Graph API</label>
      <br />
      <textarea id="status_update" name="status_update" cols="50" rows="5">
        Write your status here and click submit
      </textarea>
      <br />
      <input type="submit" value="Update My Status" />
    </form>
    <?php if( isset( $status_update ) ) : ?>
      <br />
      <strong style="color: red;">Status Updated Successfully! Status ID is : <?php echo $status_update['id']; ?></strong>
    <?php endif; ?>
  </div>
  <?php endif; ?>
</body>
</html>