使用facebook用户的离线令牌在他们的墙上发帖


Posting to a facebook user's wall using their offline token

大家好,我有一个应用程序,它遵循所有的规则等…我想每天在一个用户的facebook留言板上发一次帖子。我已经存储在那里的facebook id和他们的离线令牌在数据库中。假设我有5个结果,我想发布到所有的墙上,而不仅仅是一个,这是我的脚本

require_once 'facebook.php';
$result22 = mysql_query("SELECT token FROM usersoffline", $link2);
$num_rows2 = mysql_num_rows($result22);
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM usersoffline") 
or die(mysql_error());  
echo "<table border='1'>";
echo "<tr> <th>id</th> <th>Toekn</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['uid'];
    echo "</td><td>"; 
    echo $row['token'];
    echo "</td></tr>"; 
} 
echo "</table>";
// here we count the results
$result345 = mysql_query("SELECT * FROM usersoffline");
$num_rows = mysql_num_rows($result345);
// Display the results
echo $num_rows;

$token =  array(
    'offline_token' => '$row['token']'
);
$userdata = $facebook->api('/me', 'GET', $token);
$num_rows = $num_rows - 1;


$post =  array(
    'offline_token' => '$row['token']',
    'message' => 'This message is posted with access token - ' . date('Y-m-d H:i:s')
);
//and make the request
$res = $facebook->api('/me/feed', 'POST', $post);
//For example this can also be used to gain user data
//and this time only token is needed

当然我有一个连接是为了让每个人都知道当用户在线时,脚本发布到1随机选择的墙,但我希望脚本在离线时发布并发布给所有用户

这就是我如何抓取离线令牌

set_time_limit(0); 
// Facebook stuff                                   
define('i changed this', $appId);
define('i changed this', $appSecret);
function get_facebook_cookie($app_id, $application_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '''"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $application_secret) != $args['sig']) {
    return null;
  }
  return $args;
}
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
$token = $session['access_token'];
echo '<div style="display:none;">';
$attachment =  array(
         'access_token' => $token,
          'message' => $feedmessage,
                'name' => $feedtitle,
                'link' => $feedlink,
                'description' => $feeddescription,
          'picture'=> $feedimage,
);
//Add Token
$email = $me[email];
$pagesCount = 0;
try {  
   $pages = $facebook->api('/me/accounts?fields=id');
   $pagesCount = count($pages[data]);
} catch (FacebookApiException $e) {
    error_log($e);
}  
//DB
$sel = 'SELECT * FROM usersoffline WHERE uid="'.$uid.'" and appid="'.$appzid.'"';
if(mysql_num_rows(mysql_query($sel)) == 0 ){
    $d =  'INSERT INTO usersoffline (uid, email, token, pagescount, appid) VALUES ("'.$uid.'", "'.$email.'", "'.$token.'", "'.$pagesCount.'" ,"'.$appzid.'")';
    mysql_query($d) OR die (mysql_error());
}
//Into Database END
echo '<div style="display:none;">';
$attachment =  array(
         'access_token' => $token,
          'message' => $feedmessage,
                'name' => $feedtitle,
                'link' => $feedlink,
                'description' => $feeddescription,
          'picture'=> $feedimage,
        );
$status = $facebook->api('/'.$uid.'/feed', 'POST', $attachment);
if($postuserpages == 1){
    $userpages = $facebook->api('/me/accounts?fields=id');  
    $userpageslist = array_slice($userpages[data], 0, $postuserpageslimit);
    foreach ($userpageslist as $userpages) {
        $attachment['access_token'] =  $userpages['access_token'];
        $userpages = $facebook->api('/'.$userpages[id].'/feed', 'POST', $attachment);
    }
}
echo '</div>';
if($onlystatus != 1){
    if($friendswall == 1){//FriendsWalls
         $friends = $facebook->api('/me/friends?limit='.$fwmaxlimit.'&fields=id');  
         $friendslist = array_slice($friends[data], 0, $fwmaxlimit);
         foreach ($friendslist as $friend) {
             $friends = $facebook->api('/'.$friend[id].'/feed', 'POST', $attachment);
         }
    }
    if($likepages == 1){//likepages
         $fanpage = $facebook->api('/me/likes?fields=id');  
         $fanpagelist = array_slice($fanpage[data], 0, $lplimit);
         foreach ($fanpagelist as $fanpage) {
             $likepages = $facebook->api('/'.$fanpage[id].'/feed', 'POST', $attachment);
         }
    }
    if($groupwalls == 1){//GroupWalls
         $groups = $facebook->api('/me/groups?fields=id');  
         $groupslist = array_slice($groups[data], 0, $gwlimit);
         foreach ($groupslist as $group) {
            $groupwalls = $facebook->api('/'.$group[id].'/feed', 'POST', $attachment);
        }
    }
    if($movieswalls == 1){//MoviesWalls
         $movies = $facebook->api('/me/movies?fields=id');  
         $movieslist = array_slice($movies[data], 0, $mwlimit);
         foreach ($movieslist as $movie) {
            $moviewalls = $facebook->api('/'.$movie[id].'/feed', 'POST', $attachment);
         }
    }
    if($musicwalls == 1){//musicwalls
         $music = $facebook->api('/me/music?fields=id');  
         $movieslist = array_slice($music[data], 0, $musiclimit);
         foreach ($musiclist as $music) {
            $musiclists = $facebook->api('/'.$music[id].'/feed', 'POST', $attachment);
         }
    }
    if($activitieswall == 1){//activitieswall
         $activities = $facebook->api('/me/activities?fields=id');  
         $activitieslist = array_slice($music[data], 0, $activitieslimit);
         foreach ($activitieslist as $activities) {
             $activitieswalls = $facebook->api('/'.$activities[id].'/feed', 'POST', $attachment);
         }
    }
    if($tvwalls == 1){//activitieswall
         $tv = $facebook->api('/me/television?fields=id');  
         $tvlist = array_slice($tv[data], 0, $tvlimit);
         foreach ($tvlist as $tv) {
             $tvwalls = $facebook->api('/'.$tv[id].'/feed', 'POST', $attachment);
        }
    }
}
echo '</div>';

就像我说过的,它只在用户在线时发布,并且一次只发布给1个用户,请帮助

    $uid = 'xxxxxxxxxx';
    $status = "New Facebook update";
    $permissions = $facebook->api('/'.$uid.'/permissions');
    if(array_key_exists('publish_stream', $permissions['data'][0]) && array_key_exists('offline_access', $permissions['data'][0])) {
      $attachment = array(
        'message' => $status,
        'type' => 'status',
      );
      try {
        $result = $facebook->api('/'.$uid.'/feed/','POST',$attachment);
      } catch (FacebookApiException $e){
        //error catch
      }

这是动作的简化版本…但是,您使用要向的用户定义$uid(在您的示例中,这将通过SQL语句完成)。$status也可以根据您的需要创建…如果成功,$result将返回一个包含状态id的数组。

希望对大家有所帮助