-
-
Save gloriousnoise/d40d9d735fcd77d962097456111ab3e2 to your computer and use it in GitHub Desktop.
Twitter JSON to RSS feed proxy for Twitter API 1.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Via https://gist.github.com/Ben-Atherton/2d86c81c8f390259fd656794cde4e70e for the api auth. | |
Via https://gist.github.com/BtbN/3f59269de9487ea9f2e4cc0174f4d23c for the formatting. | |
I updated it to do rss insteaed of atom. | |
*/ | |
$token = ''; | |
$token_secret = ''; | |
$consumer_key = ''; | |
$consumer_secret = ''; | |
$user_screen_name = ''; | |
#region auth | |
$host = 'api.twitter.com'; | |
$method = 'GET'; | |
$path = '/1.1/statuses/user_timeline.json'; // api call path | |
$query = array( // query parameters | |
'screen_name' => $twitter_name, | |
'count' => 20, | |
'exclude_replies' => 'true', | |
'tweet_mode' => 'extended', | |
'contributor_details' => 'false', | |
); | |
$oauth = array( | |
'oauth_consumer_key' => $consumer_key, | |
'oauth_token' => $token, | |
'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended | |
'oauth_timestamp' => time(), | |
'oauth_signature_method' => 'HMAC-SHA1', | |
'oauth_version' => '1.0' | |
); | |
$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting | |
$query = array_map("rawurlencode", $query); | |
$arr = array_merge($oauth, $query); // combine the values THEN sort | |
asort($arr); // secondary sort (value) | |
ksort($arr); // primary sort (key) | |
// http_build_query automatically encodes, but our parameters | |
// are already encoded, and must be by this point, so we undo | |
// the encoding step | |
$querystring = urldecode(http_build_query($arr, '', '&')); | |
$url = "https://$host$path"; | |
// mash everything together for the text to hash | |
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring); | |
// same with the key | |
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret); | |
// generate the hash | |
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true))); | |
// this time we're using a normal GET query, and we're only encoding the query params | |
// (without the oauth params) | |
$url .= "?".http_build_query($query); | |
$url=str_replace("&","&",$url); //Patch by @Frewuill | |
$oauth['oauth_signature'] = $signature; // don't want to abandon all that work! | |
ksort($oauth); // probably not necessary, but twitter's demo does it | |
// also not necessary, but twitter's demo does this too | |
function add_quotes($str) { return '"'.$str.'"'; } | |
$oauth = array_map("add_quotes", $oauth); | |
// this is the full value of the Authorization line | |
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', ')); | |
// if you're doing post, you need to skip the GET building above | |
// and instead supply query parameters to CURLOPT_POSTFIELDS | |
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"), | |
//CURLOPT_POSTFIELDS => $postfields, | |
CURLOPT_HEADER => false, | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_SSL_VERIFYPEER => false); | |
// do our business | |
$feed = curl_init(); | |
curl_setopt_array($feed, $options); | |
$json = curl_exec($feed); | |
//echo ('10 ' . $json . '<br />'); | |
curl_close($feed); | |
# endregion | |
$all = json_decode($json, true); | |
if (!$all) | |
{ | |
http_response_code(400); | |
echo 'Could not decode JSON response'; | |
print_r($response); | |
} | |
$updated = date(DATE_ATOM, strtotime($all[0]['created_at'])); | |
header('Content-type: application/rss+xml; charset=UTF-8', true); | |
$image_url = $all[0]['user']['profile_image_url']; | |
?> | |
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"> | |
<channel> | |
<title>@<?php echo $user_screen_name ?> on Twitter</title> | |
<description>@<?php echo $user_screen_name ?> on Twitter</description> | |
<pubDate><?php echo date(DATE_RSS, strtotime($updated))?></pubDate> | |
<link>https://twitter.com/<?php echo $user_screen_name ?></link> | |
<atom:link href="https://<?php echo $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]?>" rel="self" type="application/rss+xml"/> | |
<image> | |
<link>https://twitter.com/<?php echo $user_screen_name ?></link> | |
<url><?php echo $image_url ?></url> | |
<title>@<?php echo $user_screen_name ?> on Twitter</title> | |
</image> | |
<?php | |
foreach($all as $row) | |
{ | |
$id = $row['id_str']; | |
$text = $row['full_text']; | |
$is_retweet = isset($row['retweeted_status']); | |
if($is_retweet) | |
{ | |
$title = $row['retweeted_status']['full_text']; | |
$entry = "RT @" . $row['retweeted_status']['user']['screen_name'] . ": " . $row['retweeted_status']['full_text']; | |
} else { | |
$title = $text; | |
$entry = $text; | |
} | |
$expansion_target = $row; | |
if($is_retweet) | |
{ | |
$expansion_target = $row['retweeted_status']; | |
} | |
if(isset($expansion_target['entities']) and isset($expansion_target['entities']['urls'])) | |
{ | |
// expand t.co URLs | |
$urls = $expansion_target['entities']['urls']; | |
foreach($urls as $url) | |
{ | |
$url_orig = $url['url']; | |
$url_expanded = $url['expanded_url']; | |
// don't link twitter and youtube urls so reader can embed them. | |
if (preg_match("/\b(twitter.com|youtube.com|youtu.be|instagram.com)\b/i", $url_expanded)) { | |
$url_expanded_tagged = "<br /><br />" . $url_expanded; | |
} else { | |
$url_expanded_tagged = "<br /><br /><a href='$url_expanded'>$url_expanded</a>"; | |
} | |
$title = str_replace($url_orig, "", $title); | |
$entry = str_replace($url_orig, $url_expanded_tagged, $entry); | |
} | |
} | |
if(isset($expansion_target['extended_entities']) and isset($expansion_target['extended_entities']['media'])) | |
{ | |
$media = $expansion_target['extended_entities']['media']; | |
foreach($media as $medium) | |
{ | |
$medium_orig = $medium['url']; | |
$medium_raw = $medium['media_url_https']; | |
$medium_display = $medium['display_url']; | |
$medium_expanded = $medium['expanded_url']; | |
$medium_expanded_tagged = "<br /><br /><a href='$medium_expanded'><img src='$medium_raw'></img></a>"; | |
$title = str_replace($medium_orig, "", $title); | |
$entry = str_replace($medium_orig, $medium_expanded_tagged, $entry); | |
} | |
} | |
$name = htmlspecialchars($row['user']['name']); | |
$screen_name = $row['user']['screen_name']; | |
$url = $row['user']['url']; | |
$profile_image_url = $row['user']['profile_image_url']; | |
$source = htmlspecialchars($row['source']); | |
//$entry = htmlspecialchars($entry); | |
$title = htmlspecialchars($title); | |
$created = date(DATE_ATOM, strtotime($row['created_at'])); | |
// replace line breaks with spaces | |
$title = trim(preg_replace('/\s+/', ' ', $title)); | |
// get first sentence | |
$title = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $title); | |
// remove final period | |
$title = trim($title, '.'); | |
?> | |
<item> | |
<title><?php echo $title?></title> | |
<link>http://twitter.com/<?php echo $screen_name?>/status/<?php echo $id?></link> | |
<description> | |
<![CDATA[ <?php echo $entry ?> ]]> | |
</description> | |
<pubDate><?php echo date(DATE_RSS, strtotime($created))?></pubDate> | |
<dc:creator><?php echo $name?></dc:creator> | |
<guid isPermaLink="false">https://twitter.com/<?php echo $screen_name?>/status/<?php echo $id?></guid> | |
</item> | |
<?php | |
} | |
?> | |
</channel> | |
</rss> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment