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 !!
// 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>';
}
?>
You are testing the value of the variable
$fbmeat the very top and in the the middle of your code.Before you invoke the Facebook API.
Before you actually populate the variable.
Both of which happen at the bottom of your code.
PHP is executed in order. You can not retrieve the value of a variable before it has been set.
Even if your code is in order, you are completely discarding any exceptions that the Facebook API may be returning.