A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

55 lines
1.4 KiB

<?php
namespace SparkLib\SocialNoise;
use \SparkLib\Util\DateTime;
class Reddit extends \SparkLib\SocialNoise {
/**
* Get an object representing search results.
*/
public static function search ($text, $qty)
{
$url = "http://www.reddit.com/search.json?q={$text}&sort=new";
return static::getSearchFromJson($url);
}
/**
* Return search results formatted as HTML.
*
* @param $text to search for
* @param $qty max number of items to return
*/
public static function searchHTML ($text, $qty)
{
$h = function ($str) { return htmlspecialchars($str); };
$html = '<ul>';
$result = static::search($text, $qty);
$i = 0;
foreach ($result->data->children as $post) {
if ($i++ > $qty) {
break;
}
$post_title = $h(trim($post->data->title));
if ($post->data->score >= 10)
$post_title = "<b>$post_title</b>";
$html .= '<li><a href="http://www.reddit.com/'
. $h($post->data->permalink)
. '">'
. $post_title . "</a>";
$author = $post->data->author;
$html .= ' <small><i>' . $post->data->score . ' points, submitted '
. DateTime::contextualTime($post->data->created_utc)
. ' by <a href="http://www.reddit.com/u/'
. $h($author) . '">' . $h($post->data->author) . '</a></i></small></li>';
}
return $html . '</ul>';
}
}