Skip to main content

Get Player Feed

Retrieve social feed data for a specific app, including player posts, achievements, tournament results, and other social activities.

Method

getFeedData(string $appID, array $feedType, string $reqPlayerID = '', int $maxResults = 25, int $loadedCount = 0, int $postsBy = Constants::FEED_POST_BY_ALL, array $loadedLeaderboardIDs = [], array $loadedTournamentIDs = []): array

Parameters

ParameterTypeRequiredDescription
$appIDstringYesThe app ID for which to fetch the feed
$feedTypearrayYesArray of feed types to fetch (see Feed Types below)
$reqPlayerIDstringNoPlayer ID from whose perspective to load feed (default: empty)
$maxResultsintNoMaximum number of feed items to fetch (default: 25)
$loadedCountintNoNumber of feeds already loaded for pagination (default: 0)
$postsByintNoFilter posts by source (default: FEED_POST_BY_ALL)
$loadedLeaderboardIDsarrayNoLeaderboard IDs to exclude from results
$loadedTournamentIDsarrayNoTournament IDs to exclude from results

Feed Types

Use these constants with $feedType parameter:

ConstantValueDescription
Constants::FEED_TYPE_BEAT_SCORE0Score beating achievements
Constants::FEED_TYPE_HIGH_SCORE1High score achievements
Constants::FEED_TYPE_POST_ALL2All types of posts
Constants::FEED_TYPE_POST_TEXT_ONLY3Text-only posts
Constants::FEED_TYPE_POST_IMAGE_ONLY4Image-only posts
Constants::FEED_TYPE_POST_VIDEO_ONLY5Video-only posts
Constants::FEED_TYPE_LEADEBOARD7Leaderboard-related posts
Constants::FEED_TYPE_TOURNAMENT8Tournament-related posts
Constants::FEED_TYPE_FEATURED9Featured content

Post Filters

Use these constants with $postsBy parameter:

ConstantValueDescription
Constants::FEED_POST_BY_REQUESTING_PLAYER0Posts by the requesting player only
Constants::FEED_POST_BY_FRIENDS1Posts by friends only
Constants::FEED_POST_BY_ALL2Posts by all players (default)

Return Value

Returns an array with the following structure:

[
'players' => Player[], // Array of Player objects
'data' => FeedItem[] // Array of FeedItem objects
]

Example Usage

Basic Feed Retrieval

use Veniso\Moitribe\Sdk\MoitribeApi;
use Veniso\Moitribe\Sdk\modules\classes\Constants;

$moitribe = new MoitribeApi(['apiKey' => 'your-api-key']);

// Get all types of feed posts for an app
$feedTypes = [
Constants::FEED_TYPE_POST_ALL,
Constants::FEED_TYPE_TOURNAMENT,
Constants::FEED_TYPE_LEADEBOARD
];

$feedData = $moitribe->feeds()->getFeedData(
'your-app-id',
$feedTypes
);

foreach ($feedData['data'] as $feedItem) {
echo "Feed ID: " . $feedItem->getId() . "\n";
echo "Type: " . $feedItem->getType() . "\n";
echo "Content: " . $feedItem->getText() . "\n";
echo "Timestamp: " . $feedItem->getTimestamp() . "\n";
echo "---\n";
}

Player-Specific Feed

// Get feed from player's perspective
$feedData = $moitribe->feeds()->getFeedData(
'your-app-id',
[Constants::FEED_TYPE_POST_ALL],
'player-123', // Requesting player ID
20, // Max results
0, // Loaded count
Constants::FEED_POST_BY_FRIENDS // Only friends' posts
);

Paginated Feed Loading

function loadFeedPaginated($moitribe, $appId, $page = 0, $pageSize = 25) {
$feedTypes = [Constants::FEED_TYPE_POST_ALL];
$loadedCount = $page * $pageSize;

$feedData = $moitribe->feeds()->getFeedData(
$appId,
$feedTypes,
'', // No specific player perspective
$pageSize, // Max results
$loadedCount // Already loaded count
);

return $feedData;
}

// Load first page
$page1 = loadFeedPaginated($moitribe, 'your-app-id', 0, 25);

// Load second page
$page2 = loadFeedPaginated($moitribe, 'your-app-id', 1, 25);

Tournament and Leaderboard Feed Only

// Get only tournament and leaderboard related posts
$tournamentFeed = $moitribe->feeds()->getFeedData(
'your-app-id',
[
Constants::FEED_TYPE_TOURNAMENT,
Constants::FEED_TYPE_LEADEBOARD
],
'', // All players
50, // More results for competitive content
0, // Start from beginning
Constants::FEED_POST_BY_ALL
);

Feed with Exclusions

// Exclude already seen tournaments and leaderboards
$excludedTournaments = ['tourn-1', 'tourn-2'];
$excludedLeaderboards = ['ldr-1'];

$feedData = $moitribe->feeds()->getFeedData(
'your-app-id',
[Constants::FEED_TYPE_POST_ALL],
'',
25,
0,
Constants::FEED_POST_BY_ALL,
$excludedLeaderboards,
$excludedTournaments
);

Error Handling

try {
$feedData = $moitribe->feeds()->getFeedData(
'your-app-id',
[Constants::FEED_TYPE_POST_ALL]
);

if (empty($feedData['data'])) {
echo "No feed items found\n";
} else {
echo "Found " . count($feedData['data']) . " feed items\n";
}

} catch (InvalidArgumentException $e) {
echo "Invalid argument: " . $e->getMessage() . "\n";
} catch (Exception $e) {
echo "Error fetching feed: " . $e->getMessage() . "\n";
}

Integration Examples

Web API Endpoint

// routes/web.php
use Illuminate\Http\Request;

Route::get('/api/feed/{appId}', function (Request $request, $appId) {
$moitribe = new MoitribeApi(['apiKey' => config('moitribe.api_key')]);

$feedTypes = $request->input('types', [
Constants::FEED_TYPE_POST_ALL,
Constants::FEED_TYPE_TOURNAMENT
]);

$playerId = $request->input('player_id', '');
$maxResults = $request->input('limit', 25);
$page = $request->input('page', 0);

try {
$feedData = $moitribe->feeds()->getFeedData(
$appId,
$feedTypes,
$playerId,
$maxResults,
$page * $maxResults
);

return response()->json([
'success' => true,
'data' => $feedData['data'],
'players' => $feedData['players'],
'pagination' => [
'page' => $page,
'limit' => $maxResults,
'has_more' => count($feedData['data']) >= $maxResults
]
]);

} catch (Exception $e) {
return response()->json([
'success' => false,
'error' => $e->getMessage()
], 500);
}
});

Real-time Feed Updates

class FeedService {
private $moitribe;
private $cache;

public function __construct($moitribe, $cache) {
$this->moitribe = $moitribe;
$this->cache = $cache;
}

public function getFreshFeed($appId, $playerId = '') {
$cacheKey = "feed:{$appId}:" . ($playerId ?: 'global');

// Try to get from cache first
$cachedFeed = $this->cache->get($cacheKey);
if ($cachedFeed) {
return $cachedFeed;
}

// Fetch fresh data
$feedData = $this->moitribe->feeds()->getFeedData(
$appId,
[Constants::FEED_TYPE_POST_ALL],
$playerId,
50
);

// Cache for 5 minutes
$this->cache->set($cacheKey, $feedData, 300);

return $feedData;
}

public function invalidateFeedCache($appId, $playerId = '') {
$cacheKey = "feed:{$appId}:" . ($playerId ?: 'global');
$this->cache->delete($cacheKey);
}
}

Best Practices

  1. Pagination: Use $loadedCount and $maxResults for efficient pagination
  2. Filtering: Specify only the feed types you need to reduce response size
  3. Caching: Cache feed data for short periods to improve performance
  4. Player Context: Use $reqPlayerID to personalize feed content
  5. Exclusions: Use $loadedLeaderboardIDs and $loadedTournamentIDs to avoid duplicates
  • Feed Comments - Comment on feed items, load comments, and like/unlike feed items

Feed Item Structure

Each FeedItem in the response contains:

  • id - Unique feed item identifier
  • type - Type of feed content
  • text - Text content of the post
  • timestamp - When the item was created
  • playerId - ID of the player who created it
  • likes - Number of likes
  • comments - Number of comments
  • Additional metadata based on feed type

Player Data Structure

The players array contains Player objects for all players referenced in the feed, allowing you to display player information without additional API calls.