rezozero / mixedfeed
A PHP library to get social networks feeds and merge them
Installs: 24 869
Dependents: 0
Suggesters: 0
Security: 0
Stars: 112
Watchers: 16
Forks: 19
Open Issues: 1
Requires
- php: >=7.4
- ext-json: *
- abraham/twitteroauth: ^3.0
- guzzlehttp/guzzle: ^7.0
- guzzlehttp/promises: ^1.5
- guzzlehttp/psr7: ^2.1
- psr/cache: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- jms/serializer: ^2.1 || ^3.5
- phpstan/phpstan: ^1.0
- squizlabs/php_codesniffer: ^3.3
- symfony/cache: ^5.0
- symfony/dotenv: ^5.0
- symfony/stopwatch: ^5.0
- dev-master
- v4.0.1
- v4.0.0
- v3.x-dev
- v3.2.1
- v3.2.0
- 3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.9
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4.1
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- dev-develop
- dev-feature/linkedin
- dev-feature/yelp
This package is auto-updated.
Last update: 2024-10-25 13:04:06 UTC
README
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
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
stdClass
to 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
Modify cache TTL
Each feed-provider which inherits from AbstractFeedProvider
has access to setTtl()
method in order to modify the default cache time.
By default it is set for 7200
seconds, so you can adjust it to invalidate doctrine cache more or less often.
Create your own feed provider
There are plenty of APIs on the internet, and this tool won’t be able to handle them all.
But this is not a problem, you can easily create your own feed provider in mixedfeed. You just have to create a new class that
will inherit from RZ\MixedFeed\AbstractFeedProvider
. Then you will have to implement some methods from FeedProviderInterface
:
getRequests($count = 5): \Generator
method which return a GuzzleRequest
generator to be transformed to a response. This is the best option as it will enable async request pooling.supportsRequestPool(): bool
method should return if your provider can be pooled to enhance performances. If you are using a third party library to fetch your data (such as some platform SDK), you should set it tofalse
.createFeedItemFromObject($item)
method which transform a raw feed object into a canonicalRZ\MixedFeed\Canonical\FeedItem
andRZ\MixedFeed\Canonical\Image
getDateTime
method to look for the critical datetime field in your feed.getFeed
method to consume your API endpoint with a count limit and take care of caching your responses. This method must convert your own feed items into\stdClass
objects, not arrays.getCanonicalMessage
method to look for the important text content in your feed items.getFeedPlatform
method to get a global text identifier for your feed items.- then a constructor that will be handy to use directly in the MixedFeed initialization.
Feel free to check our existing Feed providers to see how they work. And we strongly advise you to implement a caching system not to call your API endpoints at each request. By default, we use Doctrine’s caching system which has many storage options.
Create a feed provider from a Doctrine repository
If you need to merge social network feeds with your own website articles, you can create a custom FeedProvider which wraps your Doctrine objects into \stdClass
items. You’ll need to implement your getFeed
method using an EntityManager:
protected $entityManager; public function __construct(\Doctrine\ORM\EntityManagerInterface $entityManager) { $this->entityManager = $entityManager; } protected function getFeed($count = 5) { return array_map( function (Article $article) { $object = new \stdClass(); $object->native = $article; return $object; }, $this->entityManager->getRepository(Article::class)->findBy( [], ['datetime' => 'DESC'], $count ) ); } protected function createFeedItemFromObject($item) { $feedItem = new RZ\MixedFeed\Canonical\FeedItem(); $feedItem->setDateTime($this->getDateTime($item)); $feedItem->setMessage($this->getCanonicalMessage($item)); $feedItem->setPlatform($this->getFeedPlatform()); for ($item->images as $image) { $feedItemImage = new RZ\MixedFeed\Canonical\Image(); $feedItemImage->setUrl($image->url); $feedItem->addImage($feedItemImage); } return $feedItem; }
Then you can define your date-time and canonical message methods to look into this object:
/** * @inheritDoc */ public function getDateTime($item) { if ($item->native instanceof Article) { return $item->native->getDatetime(); } return null; } /** * @inheritDoc */ public function getCanonicalMessage(stdClass $item) { if ($item->native instanceof Article) { return $item->native->getExcerpt(); } return null; }