Skip to content

Commit

Permalink
Fixed RSS for Validation + workaround for better facebook integration
Browse files Browse the repository at this point in the history
- Improved RSS validation (pubDate + html content)
- If your rss feed is to be parsed by facebook, it will display twitter
default meta description as link description text... Not very useful.
Set the provided constant to true to enable a workaround using the
a_tweet.php file.
  • Loading branch information
pixeline committed Aug 22, 2013
1 parent 0749cda commit 666507d
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 8 deletions.
90 changes: 90 additions & 0 deletions a_tweet.php
@@ -0,0 +1,90 @@
<?php
/*
fetches a given tweet, and renders an html page using the tweet's content. Useful to control how facebook publishes a link to the tweet (if just using the twitter status url, the description is Twitter's default description meta tag, yuk!).
*/

error_reporting(E_WARNING | E_ERROR);
ini_set('display_errors',1);

$tid = null;

if(!isset($_GET['tid'])){
die("nope, sorry, no tweet specified");
}

$tid = trim($_GET['tid']);

require 'tmhOAuth/tmhOAuth.php';
require 'twitter_auth.php';

$tmhOAuth = new tmhOAuth($twitter_auth);

$url = "/1.1/statuses/show.json";
$options = array(
'id'=> $tid,
);

// DO !

$code = $tmhOAuth->request('GET', $tmhOAuth->url($url), $options);
$tweet = json_decode($tmhOAuth->response['response'], false);


$title= htmlspecialchars(htmlspecialchars_decode($tweet->user->name.": ".strip_tags($tweet->text)));
$description= htmlspecialchars(htmlspecialchars_decode(strip_tags($tweet->text)));
$description_html = find_hashtags(find_links($description));

$url = htmlspecialchars("https://twitter.com/".$tweet->user->screen_name."/statuses/".$tweet->id_str);;
$image = (strlen($tweet->entities->media[0]->media_url)>0) ? htmlspecialchars($tweet->entities->media[0]->media_url) : null;
$datetime = new DateTime($tweet->created_at);
$datetime->setTimezone(new DateTimeZone('Europe/Brussels'));
$created_at = $datetime->format(DATE_RFC822);
$profile_url= $tweet->user->profile_image_url;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title ?></title>

<!-- Meta Information starts -->
<meta name="description" content="<?php echo $description ?>">

<!-- Open graph starts -->
<meta property="og:title" content="<?php echo $title ?>">
<meta property="og:type" content="website">
<meta property="og:url" content="<?php echo $_SERVER['PHP_SELF'] ?>">
<meta property="og:image" content="<?php echo $image ?>">
<meta property="og:description" content="<?php echo $description ?>">
<!-- Open graph ends -->
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.2.1/pure-min.css">
<style type="text/css">
body{background:#DDD;}
h1{font-size:100%;}
article{background:white;max-width:25em;margin:2em auto; padding:2em;border:1px solid #CCC;border-radius:1em}
img{max-width: 100%;border:1px solid #CCC;padding:4px;vertical-align: text-bottom;}

</style>
</head>

<body>
<article>
<h1><?php echo '<img src="'.$profile_url.'"> '. $description_html ?></h1>
<p><?php echo ($image !='') ? '<img src="'.$image.'">':'';?></p>
<p>Source: <a href="<?php echo $url ?>" rel="nofollow">twitter</a></p>
</article>
</body>
</html>
<?php
// helpers
function find_links($text){
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// make the urls hyper links
return (preg_match($reg_exUrl, $text, $url)) ? preg_replace($reg_exUrl, '<a href="'.$url[0].'">'.$url[0].'</a> ', $text) : $text;
}

function find_hashtags($text){
// https://twitter.com/search?q=%23dwmaj&src=hash
return preg_replace("/#(\w+)/i", "<a href=\"http://twitter.com/search?q=%23$1\">#$1</a>", $text);
}
?>
47 changes: 39 additions & 8 deletions twitter_json_to_rss.php
Expand Up @@ -3,6 +3,16 @@
require 'tmhOAuth/tmhOAuth.php';
require 'twitter_auth.php';

define('THIS_URL', 'http://'.$_SERVER['HTTP_HOST']);

define('THIS_PATH',dirname($_SERVER["REQUEST_URI"]));

// If your rss feed is to be parsed by facebook, it will display twitter default meta description as link description text... Not very useful. Set this to true to use a custom page, which will use the tweet's message as description.

define('USE_CUSTOM_TWEET_FILE', false);

// RUNTIME

$tmhOAuth = new tmhOAuth($twitter_auth);

$screen_name = null;
Expand Down Expand Up @@ -54,6 +64,7 @@

$return = json_decode($tmhOAuth->response['response'], false);
$now = date("D, d M Y H:i:s T");
$now = rfc822Date($now);
$link = htmlspecialchars('http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
header("Content-Type: application/xml; charset=UTF-8");
?>
Expand All @@ -67,17 +78,37 @@
<lastBuildDate><?php echo $now; ?></lastBuildDate>
<?php
$tweets = ($usage == 'hashtag') ? $return->statuses : $return;
foreach ($tweets as $line){ ?>
foreach ($tweets as $line){
$title= htmlspecialchars(htmlspecialchars_decode($line->user->name.": ".strip_tags($line->text)));
$description= htmlspecialchars(htmlspecialchars_decode(strip_tags($line->text)));
$url = htmlspecialchars("https://twitter.com/".$line->user->screen_name."/statuses/".$line->id_str);;
$image = (strlen($line->entities->media[0]->media_url)>0) ? htmlspecialchars($line->entities->media[0]->media_url) : null;
$created_at = rfc822Date($line->created_at);

?>
<item>
<title><?php echo htmlspecialchars(htmlspecialchars_decode($line->user->name.": ".strip_tags($line->text))); ?></title>
<description><?php echo htmlspecialchars(htmlspecialchars_decode(strip_tags($line->text))); ?></description>
<pubDate><?php echo $line->created_at ?></pubDate>
<guid><?php echo htmlspecialchars("https://twitter.com/".$line->user->screen_name."/statuses/".$line->id_str); ?></guid>
<link><?php echo htmlspecialchars("https://twitter.com/".$line->user->screen_name."/statuses/".$line->id_str); ?></link>
<?php echo (strlen($line->entities->media[0]->media_url)>0) ? '<image>'.htmlspecialchars($line->entities->media[0]->media_url).'</image>'."\n": ''; ?>
<title><?php echo $title; ?></title>
<description>
<![CDATA[
<?php
echo $description;
if (strlen($line->entities->media[0]->media_url)>0) { ?>
<img src="<?php echo $image; ?>">
<?php
}
?> ]]></description>
<pubDate><?php echo $created_at ?></pubDate>
<guid><?php echo $url; ?></guid>
<link><?php echo (USE_CUSTOM_TWEET_FILE) ? THIS_URL.THIS_PATH.'/a_tweet.php?tid='.$line->id_str : $url; ?></link>
</item>
<?php
}
?>
</channel>
</rss>
</rss>
<?php
function rfc822Date($str){
$timestamp = strtotime($str);
return date(DATE_RSS, $timestamp);
}
?>

0 comments on commit 666507d

Please sign in to comment.