1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Facebook php -Post On Users Behalf-

Discussion in 'FaceBook' started by wb919, Jun 25, 2012.

Tags:
  1. wb919

    wb919 Newbie

    Joined:
    Oct 11, 2011
    Messages:
    2
    Likes Received:
    0
    This is the code I have, I am trying to get Facebook Class, but I seem to be messing it up
    When i put place it in index.php for my app "NOthing happens" before I wasn't trying to get the facebook class, The code wouldnt recognize "new Facebook"
    I am trying to get the FB class.
    I have read the developers tips but still confused...
    $facebook = new Facebook(array( THAT IS GIVING ME THE ERROR, I AM TRYING TO GET THE FACEBOOK CLASS SO I CAN POST ON THE USERS BEHALF! =]
    -Noob Friendly Please_


    Code:
    This is the code I have, I am trying to get Facebook Class, but I seem to be messing it up
    When i put place it in index.php for my app "NOthing happens" before I wasn't trying to get the facebook class, The code wouldnt recognize "new Facebook"
    I am trying to get the FB class.
    I have read the developers tips but still confused...
    $facebook = new Facebook(array( THAT IS GIVING ME THE ERROR, I AM TRYING TO GET THE FACEBOOK CLASS SO I CAN POST ON THE USERS BEHALF! =]
    -Noob Friendly Please_
    
    
    [CODE]
    <html>
    <?php
    
    require 'facebook.php';
    
    // Create our application instance
    // (replace this with your appId and secret).
    $facebook = new Facebook(array(
    'appId'  => 'xxxxxxx',
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxx',
    ));
    
    // 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;
    }
    }
    
    // Login or logout url will be needed depending on current user state.
    if ($user) {
    $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="">
    <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>
    <h1>php-sdk</h1>
    
    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>
    
    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>
    
    <?php if ($user): ?>
      <h3>You</h3>
      <img src="code<?php echo $user; ?>/picture">
    
      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>
    
    <h3>Public profile of Naitik</h3>
    <img src="code">
    <?php echo $naitik['name']; ?>
    </body>
    
    <?php
    
           $facebook = new Facebook(array(
            'appId'  => 329394273803436, // YOUR APP ID
            'secret' => df6e14ef537ad57bdbec5bf5a9d21c57, // YOUR API SECRET
             ));
    
        $user = $facebook->getUser();
    
        if($user) {
        $attachment = array(
        'name' => 'NAME',
         'caption' => 'CAPTION',
         'link' => 'code',
         'description' => 'DESCRIPTION',
         'picture' => ''
         );
         try { $result = $facebook->api('/me/feed/', 'post',  $attachment,array('access_token'=>$valid_access_token));
    }
    catch(FacebookApiException $e) {  }
         }
      ?>
      </html>
    [/CODE]