Mixedfeed
A PHP library to rule social-feeds, to entangle them with magic, a PHP library to gather them and bind them in darkness
Install / Use
/learn @rezozero/MixedfeedREADME
mixedfeed
A PHP library to rule social-feeds, to entangle them with magic, a PHP library to gather them and bind them in darkness
- Use standalone Docker server
- Install as library
- Combine feeds
- Use FeedItem instead of raw feed
- Feed providers
- Modify cache TTL
- Create your own feed provider
Use standalone Docker server
docker pull rezozero/mixedfeed
docker run -p 8080:80 \
-e MF_FACEBOOK_PAGE_ID="xxx" \
-e MF_FACEBOOK_ACCESS_TOKEN="xxxx" \
-e MF_INSTAGRAM_USER_ID="xxx" \
-e MF_INSTAGRAM_ACCESS_TOKEN="xxxx" \
-e MF_CACHE_PROVIDER="apcu" \
-e MF_FEED_LENGTH="30" \
rezozero/mixedfeed
or use docker-compose: copy docker-compose.yml to docker-compose.test.yml and fill your provider credentials in
it. Then execute docker-compose -f docker-compose.test.yml up -d --force-recreate, Mixedfeed will be available at
http://localhost:8080
Available environment variables
| Name | Default value | Multiple? (comma separated) |
| ----------------- | ------------- | --------------------------- |
| MF_CACHE_PROVIDER | array | |
| MF_FEED_LENGTH | 12 | |
| MF_FACEBOOK_PAGE_ID | | ✅ |
| MF_FACEBOOK_ACCESS_TOKEN | | |
| MF_FACEBOOK_FIELDS | from,link,picture,full_picture,message,story,type,created_time,source,status_type | ✅ |
| MF_FACEBOOK_ENDPOINT | https://graph.facebook.com/v2.12/ | |
| MF_GRAPH_INSTAGRAM_USER_ID | | ✅ |
| MF_GRAPH_INSTAGRAM_ACCESS_TOKEN | | ✅ |
| MF_GITHUB_RELEASES_REPOSITORY | | ✅ |
| MF_GITHUB_COMMITS_REPOSITORY | | ✅ |
| MF_GITHUB_ACCESS_TOKEN | | |
| MF_MEDIUM_USERNAME | | ✅ |
| MF_MEDIUM_USER_ID | Use same order as in MF_MEDIUM_USERNAME | ✅ |
| MF_PINTEREST_BOARD_ID | | ✅ |
| MF_PINTEREST_ACCESS_TOKEN | | |
| MF_INSTAGRAM_OEMBED_ID | | ✅ |
| MF_TWITTER_SEARCH_QUERY | | |
| MF_TWITTER_USER_ID | | ✅ |
| MF_TWITTER_ACCESS_TOKEN | | |
| MF_TWITTER_ACCESS_TOKEN_SECRET | | |
| MF_TWITTER_CONSUMER_KEY | | |
| MF_TWITTER_CONSUMER_SECRET | | |
| MF_TWITTER_EXTENDED_MODE | 0 | |
| MF_YOUTUBE_PLAYLIST_ID | | ✅ |
| MF_YOUTUBE_API_KEY | | |
Install as library
mixedfeed v3+ needs at least PHP 7.2, check your server configuration.
composer require rezozero/mixedfeed
use RZ\MixedFeed\MixedFeed;
use RZ\MixedFeed\GraphInstagramFeed;
use RZ\MixedFeed\TwitterFeed;
use RZ\MixedFeed\TwitterSearchFeed;
use RZ\MixedFeed\FacebookPageFeed;
use RZ\MixedFeed\GithubReleasesFeed;
use RZ\MixedFeed\GithubCommitsFeed;
$feed = new MixedFeed([
new GraphInstagramFeed(
'instagram_user_id',
'instagram_access_token',
null ,// you can add a doctrine cache provider
[] // And a fields array to retrieve too
),
new TwitterFeed(
'twitter_user_id',
'twitter_consumer_key',
'twitter_consumer_secret',
'twitter_access_token',
'twitter_access_token_secret',
null, // you can add a doctrine cache provider
true, // exclude replies true/false
false, // include retweets true/false
false // extended mode true/false
),
new TwitterSearchFeed(
[
'#art', // do not specify a key for string searchs
'from' => 'rezo_zero',
'since' => '2015-11-01',
'until' => '2015-11-30',
],
'twitter_consumer_key',
'twitter_consumer_secret',
'twitter_access_token',
'twitter_access_token_secret',
null, // you can add a doctrine cache provider
false // extended mode true/false
),
new FacebookPageFeed(
'page-id',
'app_access_token',
null, // you can add a doctrine cache provider
[], // And a fields array to retrieve too
null // A specific Graph API Endpoint URL
),
new GithubCommitsFeed(
'symfony/symfony',
'access_token',
null // you can add a doctrine cache provider
),
new GithubReleasesFeed(
'roadiz/roadiz',
'access_token',
null // you can add a doctrine cache provider
),
new \RZ\MixedFeed\YoutubePlaylistItemFeed(
'your_playlist_id',
'api_key',
null // you can add a doctrine cache provider
),
]);
return $feed->getItems(12);
// Or use canonical \RZ\MixedFeed\Canonical\FeedItem objects
// for a better compatibility and easier templating with multiple
// social platforms.
return $feed->getAsyncCanonicalItems(12);
Combine feeds
mixedfeed can combine multiple social feeds so you can loop over them and use some common data fields such as feedItemPlatform, normalizedDate and canonicalMessage. mixedfeed will sort all your feed items by descending normalizedDate, but you can configure it to sort ascending:
new MixedFeed([…], MixedFeed::ASC);
Each feed provider must inject these three parameters in feed items:
feedItemPlatform: This is your social network name as a string i.e. «twitter». It will be important to cache your feed and for your HTML template engine to render properly each feed item.
For example, if you are using Twig, you will be able to include a sub-template for each social-platform.
{% for socialItem in mixedFeedItems %}
{% include ‘social-blocks/‘ ~ socialItem.feedItemPlatform ~ ‘.html.twig’ %}
{% endfor %}
normalizedDate: This is a critical parameter as it allows mixedfeed to sort reverse chronologically multiple feeds with heterogeneous structures.canonicalMessage: This is a useful field which contains the text content for each item over all platforms. You can use this to display items texts within a simple loop.
Use FeedItem instead of raw feed
If you need to serialize your MixedFeed to JSON or XML again, you should not want all the raw data contained in each
social feed item. So you can use the $feed->getAsyncCanonicalItems(12); method instead of getItems to get a more concise
object with essential data: RZ\MixedFeed\Canonical\FeedItem. FeedItem will provide these fields:
- id
string - platform
string - author
string - link
string - title
string - message
string - likeCount
int|null - shareCount
int|null: Share, comments or retweet count depending on platform. - images
Image[]- url
string - width
integer - height
integer
- url
- dateTime
DateTime - tags
array(only used withMediumFeed) - raw
stdClassto access raw API object if canonical item fields are not enough
When FeedItem has images, FeedItem::$images will hold an array of RZ\MixedFeed\Canonical\Image objects to
have better access to its url, width and height if they're available.
Each feed provider must implement how to hydrate a FeedItem from the raw feed overriding createFeedItemFromObject()
method.
Feed providers
| Feed provider class | Description | feedItemPlatform |
| -------------- | ---------------- | ------------------ |
| MediumFeed | Call over https://medium.com/username/latest endpoint. It only needs a $username and an optional $userId for better consistency over requests (Medium seems to apply cache on their username requests even after changing a query parameter, i.e. post limit). Medium allows maximum 14 posts per requests. | medium |
| InstagramOEmbedFeed | Call over https://api.instagram.com/oembed/ endpoint. It only needs a $embedUrls array | instagram_oembed |
| GraphInstagramFeed | Call over graph.instagram.com/$userId/media endpoint with Basic Display API. It needs a $userId and an $accessToken. Warning: Access token must be refreshed every 60 days, use RefreshInstagramAccessToken | instagram |
| ~~InstagramFeed~~ | Deprecated: Call over /v1/users/$userId/media/recent/ endpoint. It needs a $userId and an $accessToken | instagram |
| TwitterFeed | Call over statuses/user_timeline endpoint. It requires a $userId, a $consumerKey, a $consumerSecret, an $accessToken and an $accessTokenSecret. Be careful, this endpoint can only return up to 3,200 of a user’s most recent Tweets, your item count could be lesser than expected. In the same way, Twitter removes retweets after retrieving the items count. | twitter |
| TwitterSearchFeed | Call over search/tweets endpoint. It requires a $queryParams array, a $consumerKey, a $consumerSecret, an $accessToken and an $accessTokenSecret. Be careful, Twitter API won’t retrieve tweets older than 7 days, your item count could be lesser than expected. $queryParams must be a key-valued array with query operators according to Twitter API documentation. | twitter |
| FacebookPageFeed | Call over https://graph.facebook.com/v3.3/$pageId/posts endpoint by default. Endpoint can be changed using $apiBaseUrl parameter. It requires a $pageId and an $accessToken. This feed provider only works for public Facebook pages. To get an access-token visit: https://developers.facebook.c

