找到你要的答案

Q:Facebook Graph API pulling user likes

Q:脸谱网图形API拉动用户喜欢

I'm trying to pull a user's facebook likes. If I make the call for each user individually then the call is successful, but if I loop through the users in my database then the call immediately fails. The script is as below. Any idea why this is happening. And what is the solution?

<?php

$conn = mysql_connect('***', '***', '*****');
mysql_select_db('*****', $conn);

//Application Configurations
$site_url = $webPath . 'index.php';

try {
    require_once $behindPath . "src/facebook.php";
} catch (Exception $e) {
    error_log($e);
}


// Create our application instance
$facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
        ));

//$usertoken = $facebook->setAccessToken($access_token);
// Get User ID
try {
    $user = $facebook->getUser();
} catch (Exception $e) {
    header("Location: error.php?err= Some Error has occured. Please try again Later.");
    die();
}


//if($user){
//$accessToken  = $facebook->getAccessToken(); 

//the following is the query to get the facebook ids of users whose likes have not already been pulled

$query = "SELECT uid, facebook_access_token FROM fbusers WHERE facebook_id IN(SELECT facebook_id FROM user_self WHERE device = 'ios' AND facebook_id NOT IN(SELECT facebook_id FROM user_interest) ORDER BY id DESC)";
$res = mysql_query($query);
$numrows = mysql_num_rows($res);
while ($row = mysql_fetch_array($res)) {
    $access_token = $row['facebook_access_token'];
    $uid = $row['uid'];
    echo $uid . "<br />";

    try {

        // Proceed knowing you have a logged in user who's authenticated.
        $usertoken = $facebook->setAccessToken($access_token); //echo $usertoken; exit;
    } catch (FacebookApiException $e) {
        error_log($e);
    }


/////////////////////////
//  Insert Likes
/////////////////////////
// prepare array for insertion
    $I = $me = $facebook->api('/me');
    $me_v = $facebook->api('/me/movies');
    $me_t = $facebook->api('/me/television');
    $me_b = $facebook->api('/me/books');
    $me_m = $facebook->api('/me/music');

// For movies Likes
    foreach ($me_v['data'] as $key => $value) {
        //d($value); 
        $update[] = $I['id'];
        $update[] = $value['id'];
        $update[] = $value['name'];
        $update[] = 'movie';

        $insert_intrests->execute($update);
        $update = '';
    }
// For tv shows Likes
    foreach ($me_t['data'] as $key => $value) {
        //d($value); 
        $update[] = $I['id'];
        $update[] = $value['id'];
        $update[] = $value['name'];

        $tv_str = str_replace(" ", "_", 'Tv show');
        $update[] = $tv_str;

        $insert_intrests->execute($update);
        $update = '';
    }
// For Books Likes
    foreach ($me_b['data'] as $key => $value) {
        //d($value); 
        $update[] = $I['id'];
        $update[] = $value['id'];
        $update[] = $value['name'];
        $update[] = 'book';

        $insert_intrests->execute($update);
        $update = '';
    }

// For music Likes
    foreach ($me_m['data'] as $key => $value) {
        //d($value); 
        $update[] = $I['id'];
        $update[] = $value['id'];
        $update[] = $value['name'];
        $update[] = 'music';

        $insert_intrests->execute($update);
        $update = '';
    }

/////////////////////////
//  END Insert Likes 
/////////////////////////
}
mysql_close($conn);
?>

我试图拉一个用户的脸谱网喜欢。如果我单独为每个用户调用,那么这个调用是成功的,但是如果我在数据库中循环通过用户,那么这个调用就会立即失效。脚本如下。知道为什么会这样。解决的办法是什么?

<?php

$conn = mysql_connect('***', '***', '*****');
mysql_select_db('*****', $conn);

//Application Configurations
$site_url = $webPath . 'index.php';

try {
    require_once $behindPath . "src/facebook.php";
} catch (Exception $e) {
    error_log($e);
}


// Create our application instance
$facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
        ));

//$usertoken = $facebook->setAccessToken($access_token);
// Get User ID
try {
    $user = $facebook->getUser();
} catch (Exception $e) {
    header("Location: error.php?err= Some Error has occured. Please try again Later.");
    die();
}


//if($user){
//$accessToken  = $facebook->getAccessToken(); 

/以下是查询得到的脸谱网ID的用户,喜欢尚未被拉

$query = "SELECT uid, facebook_access_token FROM fbusers WHERE facebook_id IN(SELECT facebook_id FROM user_self WHERE device = 'ios' AND facebook_id NOT IN(SELECT facebook_id FROM user_interest) ORDER BY id DESC)";
$res = mysql_query($query);
$numrows = mysql_num_rows($res);
while ($row = mysql_fetch_array($res)) {
    $access_token = $row['facebook_access_token'];
    $uid = $row['uid'];
    echo $uid . "<br />";

    try {

        // Proceed knowing you have a logged in user who's authenticated.
        $usertoken = $facebook->setAccessToken($access_token); //echo $usertoken; exit;
    } catch (FacebookApiException $e) {
        error_log($e);
    }


/////////////////////////
//  Insert Likes
/////////////////////////
// prepare array for insertion
    $I = $me = $facebook->api('/me');
    $me_v = $facebook->api('/me/movies');
    $me_t = $facebook->api('/me/television');
    $me_b = $facebook->api('/me/books');
    $me_m = $facebook->api('/me/music');

// For movies Likes
    foreach ($me_v['data'] as $key => $value) {
        //d($value); 
        $update[] = $I['id'];
        $update[] = $value['id'];
        $update[] = $value['name'];
        $update[] = 'movie';

        $insert_intrests->execute($update);
        $update = '';
    }
// For tv shows Likes
    foreach ($me_t['data'] as $key => $value) {
        //d($value); 
        $update[] = $I['id'];
        $update[] = $value['id'];
        $update[] = $value['name'];

        $tv_str = str_replace(" ", "_", 'Tv show');
        $update[] = $tv_str;

        $insert_intrests->execute($update);
        $update = '';
    }
// For Books Likes
    foreach ($me_b['data'] as $key => $value) {
        //d($value); 
        $update[] = $I['id'];
        $update[] = $value['id'];
        $update[] = $value['name'];
        $update[] = 'book';

        $insert_intrests->execute($update);
        $update = '';
    }

// For music Likes
    foreach ($me_m['data'] as $key => $value) {
        //d($value); 
        $update[] = $I['id'];
        $update[] = $value['id'];
        $update[] = $value['name'];
        $update[] = 'music';

        $insert_intrests->execute($update);
        $update = '';
    }

/////////////////////////
//  END Insert Likes 
/////////////////////////
}
mysql_close($conn);
?>
php  mysql  facebook  facebook-graph-api