使用脱机令牌发布到Facebook用户的墙上

时间:2011-09-28 17:44:24

标签: php mysql facebook

大家好,我有一个符合所有规则等的应用程序...我想每天发布一次用户的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个用户请帮助

1 个答案:

答案 0 :(得分:2)

    $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。

的数组

我希望这会有所帮助