Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ChrisHardie
Last active April 6, 2021 10:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisHardie/bbd8aeea1364d45127574362f69c25b4 to your computer and use it in GitHub Desktop.
Save ChrisHardie/bbd8aeea1364d45127574362f69c25b4 to your computer and use it in GitHub Desktop.
Example PHP script to generate an RSS feed from a user's timeline for tweets that match a certain set of criteria
<?php
/**
* Generate an RSS feed from a Twitter user's timeline
* Chris Hardie <chris@chrishardie.com>
*/
require "/path/to/vendor/autoload.php" ;
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerKey = "your_key_goes_here"; // Consumer Key
$consumerSecret = "your_secret_goes_here"; // Consumer Secret
$accessToken = "your_token_goes_here"; // Access Token
$accessTokenSecret = "your_token_secret_goes_here"; // Access Token Secret
$twitter_username = 'wearrrichmond';
$rss_output_filename = '/path/to/www/rcs-twitter.rss';
$connection = new TwitterOAuth( $consumerKey, $consumerSecret, $accessToken, $accessTokenSecret );
// Get the 10 most recent tweets from our target user, excluding replies and retweets
$statuses = $connection->get(
'statuses/user_timeline',
array(
"count" => 10,
"exclude_replies" => true,
'include_rts' => false,
'screen_name' => $twitter_username,
'tweet_mode' => 'extended',
)
);
$xml = new SimpleXMLElement( '<rss/>' );
$xml->addAttribute( 'version', '2.0' );
$channel = $xml->addChild( 'channel' );
$channel->addChild( 'title', 'Richmond Community Schools' );
$channel->addChild( 'link', 'http://www.rcs.k12.in.us/' );
$channel->addChild( 'description', 'Richmond Community Schools' );
$channel->addChild( 'language', 'en-us' );
// For each tweet returned by the API, loop through them
foreach ( $statuses as $tweet ) {
$permalink = '';
$title = '';
// We only want tweets with URLs
if ( ! empty( $tweet->entities->urls ) ) {
// Look for a usable permalink that matches our desired URL pattern, and use the last (or maybe only) one
foreach ( $tweet->entities->urls as $url ) {
if ( false !== strpos( $url->expanded_url, 'rcs.k12.in.us/files', 0 ) ) {
$permalink = $url->expanded_url;
}
}
// If we got a usable permalink, go ahead and fill out the rest of the RSS item
if ( ! empty( $permalink ) ) {
// Set the title value from the Tweet text
$title = $tweet->full_text;
// Remove links
$title = preg_replace( '/\bhttp.*\b/', '', $title );
// Remove at-mentions
$title = preg_replace( '/\@\w+\b/', '', $title );
// Remove whitespace at beginning and end
$title = trim( $title );
$item = $channel->addChild( 'item' );
$item->addChild( 'link', $permalink );
$item->addChild( 'pubDate', date( 'r', strtotime( $tweet->created_at ) ) );
$item->addChild( 'title', $title );
// For the description, include both the original Tweet text and a full link to the Tweet itself
$item->addChild( 'description', $tweet->full_text . PHP_EOL . 'https://twitter.com/' . $twitter_username . '/status/' . $tweet->id_str . PHP_EOL );
}
}
}
$rss_file = fopen( $rss_output_filename, 'w' ) or die ("Unable to open $rss_output_filename!" );
fwrite( $rss_file, $xml->asXML() );
fclose( $rss_file );
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment