0

我之前没有成功在 stackoverflow 上获得帮助,我已经知道如何使用 twitter API 提取推文以及如何使用 Graph API 提取 facebook 状态。我的问题是这个……我知道它是技术上可行,因为 klout.com 做到了。我希望能够让我网站的用户通过 Twitter 登录,获取他们关注的人的推文,这已经有效,但同时,一旦已经使用 Twitter 登录,我想给他们连接到他们的 Facebook 帐户的选项,并在他们的 twitter 推文顶部查看他们的 FACEBOOK 状态,并在同一页面上显示推文和 FB 状态。这在技术上是可行的,因为当我通过 twitter 登录 klout.com 时,它让我可以选择连接我的 facebook 帐户并查看我的 twitter 和 facebook 数据。下面是我尝试实现的代码,但它不起作用!

// 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>';
}
?>
4

1 回答 1

0

但是当我尝试 var_dump facebook 的变量以显示我是否登录时,什么都没有返回

$fbme您正在代码的最顶部和中间测试变量的值。

调用 Facebook API 之前。

实际填充变量之前。

两者都发生在代码的底部

PHP 是按顺序执行的。在设置变量之前,您无法检索变量的值。

即使您的代码有序的,您也完全放弃了 Facebook API 可能返回的任何异常。

于 2013-01-05T07:37:20.210 回答