Skip to content

Instantly share code, notes, and snippets.

@ruliarmando
Created May 15, 2013 09:50
Show Gist options
  • Save ruliarmando/5582854 to your computer and use it in GitHub Desktop.
Save ruliarmando/5582854 to your computer and use it in GitHub Desktop.
<?php
if($quote = apc_fetch('starwars')){
echo $quote;
echo ' [cached]';
}else{
$quote = 'Do, or do not. There is no try. -- Yoda, Star Wars';
echo $quote;
apc_add('starwars', $quote, 120);
}
/*
<html>
<head>
<style type="text/css">
div.outer {
border-bottom: dashed orange 1px;
padding: 4px;
clear: both;
height: 50px;
}
div.img {
float:left;
padding-right: 2px;
}
span.attrib {
font-style: italic;
}
</style>
</head>
<body>
<h2>Twitter Search</h2>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
Search term: <input type="text" name="q" />
<input type="submit" name="submit" />
</form>
<?php
// if form submitted
if (isset($_POST['submit'])):
// sanitize query terms
$q = strip_tags($_POST['q']);
// generate cache id from query term
$id = md5($q);
// check if this search already exists in cache
// use if yes, generate fresh results and add to cache if no
if (apc_exists($id)) {
$records = apc_fetch($id);
} else {
// search Twitter for query term
$result = simplexml_load_file("http://search.twitter.com/search.atom?q=$q&lang=en");
// process Atom feed of search results
$records = array();
foreach ($result->entry as $entry) {
$item['image'] = (string)$entry->link[1]['href'];
$item['owner'] = (string)$entry->author->name;
$item['uri'] = (string)$entry->author->uri;
$item['tweet'] = (string)$entry->content;
$item['time'] = date('d M Y, h:i', strtotime($entry->published));
$records[] = $item;
}
// cache for 5 minutes
apc_store($id, $records, 300);
}
// display search results
?>
<h2>Twitter Search Results for '<?php echo $q; ?>'</h2>
<?php foreach ($records as $r): ?>
<div class="outer">
<div class="img"><img width=48" height="48" src="<?php echo $r['image']; ?>" /></div>
<div><?php echo $r['tweet']; ?><br/>
<span class="attrib">By <a href="<?php echo $r['uri']; ?>"><?php echo $r['owner']; ?></a>
on <?php echo $r['time']; ?></span></div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</body>
</html>
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment