#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    How can I get both Facebook and Twitter data on the same page through API calls ?


    I haven't been successful to receive help on this one on stackoverflow before, I already know how to pull tweets using the twitter API and how to pull facebook statuses using the Graph API .My question is this one ...and I know it's technically possible because klout.com does it . I would like to be able to let users of my websites sign in through Twitter , get the tweets of the people they follow , which already works, But at the same time , once already logged in with Twitter , I'd like to give them the option to connect to their facebook accounts , and view their FACEBOOK statuses ON TOP of their tweets of twitter and display both the tweets and FB statuses on the same page .It's technically possible because when I log on klout.com through twitter, it gives me the option to connect my facebook account and see both my twitter and facebook data . Below is the code I tried to implement but it won't work !!



    Code:
    // This twitter part works once logged in through twitter.
    <?php
    session_start();
    require_once ('../madscore/twitter/twitteroauth/twitteroauth.php');
    require_once ('../madscore/twitter/config.php');
    require ('../madscore/database/connect.php');
    /* If access tokens are not available redirect to connect page. */
    if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
        header('Location: ./clearsessions.php');
    }
    /* Get user access tokens out of the session. */
    $access_token = $_SESSION['access_token'];
    /* Create a TwitterOauth object with consumer/user tokens. */
    $handle = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
    ?>
    
           // This is the part where I call the authentications service for Facebook through the require authentication.php file , which I also copied and pasted below.
            <?php
    require "../madscore/authentication.php";
    // $config['baseurl'] ="../index3.php";
    //if user is logged in and session is valid.
    if ($fbme) {
        //Retriving movies those are user like using graph api
        try {
            $movies = $facebook->api('/me/movies');
            $pages = $facebook->api('/me/likes');
        }
        catch(Exception $o) {
            d($o);
        }
        //Calling users.getinfo legacy api call example
        try {
            $param = array('method' => 'users.getinfo', 'uids' => $fbme['id'], 'fields' => 'name,current_location,profile_url', 'callback' => '');
            $userInfo = $facebook->api($param);
        }
        catch(Exception $o) {
            d($o);
        }
        //update user's status using graph api
        if (isset($_POST['tt'])) {
            try {
                $statusUpdate = $facebook->api('/me/feed', 'post', array('message' => $_POST['tt'], 'cb' => ''));
            }
            catch(FacebookApiException $e) {
                d($e);
            }
        }
        //fql query example using legacy method call and passing parameter
        try {
            //get user id
            $uid = $facebook->getUser();
            //or you can use $uid = $fbme['id'];
            $fql = "SELECT pic_square
            FROM user 
            WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
            $param = array('method' => 'fql.query', 'query' => $fql, 'callback' => '');
            $fqlResult = $facebook->api($param);
        }
        catch(Exception $o) {
            d($o);
        }
    }
    ?>
    
            // This displays my twitter followers , and it works .. but below this code when i try to actually display data from facebook , nothing happens ..
            <?php
    $followers = $handle->get('friends/list', array('screen_name' => $screen_name));
    $json = json_encode($followers);
    $array = json_decode($json, true);
    shuffle($array);
    if (is_array($array)) {
        foreach ($array as $value) {
            if (is_array($value)) {
                foreach ($value as $key => $second) {
                    if (is_array($second)) {
                        foreach ($second as $key_second => $third) if ($key_second != 'profile_image_url') {
                            unset($key_second);
                        } else {
                            echo "<img src='" . $third . "' width='100' height='100'/>";
                        }
                    }
                }
            }
        }
    }
    ?>
    
    
    
    
    
     //Testing if facebook data gets returned here .. nothing happens ..
    
    <?php var_dump($fbme); ?>
    
    //this is the authentication file from the require statement for facebook(authentication.php)
    
    <?php
    $fbconfig['appid'] = "314216702389099";
    $fbconfig['api'] = "314286708589099";
    $fbconfig['secret'] = "8f803e0f9e9da4f2ba9f23ad3bd00ded";
    try {
        include_once "../madscore/facebook/facebook-sdk/src/facebook.php";
    }
    catch(Exception $o) {
        echo '<pre>';
        print_r($o);
        echo '</pre>';
    }
    // Create our Application instance.
    $facebook = new Facebook(array('appId' => $fbconfig['appid'], 'secret' => $fbconfig['secret'], '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->getUser();
    $fbme = null;
    // Session based graph API call.
    if ($session) {
        try {
            $uid = $facebook->getUser();
            $fbme = $facebook->api('/me');
        }
        catch(FacebookApiException $e) {
            // d($e);
    
        }
    }
    function d($d) {
        echo '<pre>';
        print_r($d);
        echo '</pre>';
    }
    ?>
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,001
    Rep Power
    9398
    "On the same page" is literally what they do. There's no magical API call or something (at least that I've ever heard of): they sign you in to Twitter, grab information, sign you in to Facebook, grab information, and simply display it all at once.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by requinix
    "On the same page" is literally what they do. There's no magical API call or something (at least that I've ever heard of): they sign you in to Twitter, grab information, sign you in to Facebook, grab information, and simply display it all at once.
    Yes , so I tried to do it in the script above but it's only displaying the twitter DATA , not the facebook data .. where did I go wrong ? That's the help I was asking for ... I know it's technically possible because klout.com does it ... I tried to grab data from facebook but it's not displaying anything , only the twitter data.. I need help about the script above. Thanks in advance
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,001
    Rep Power
    9398
    Well at first glance I'm wondering where $facebook comes from...

    Get them working independently first, so you know you can retrieve the data properly. After that then you can stitch them together.

IMN logo majestic logo threadwatch logo seochat tools logo