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.
 

43 lines
1.2 KiB

<?php
namespace SparkLib;
/**
* A base class for simple wrappers to talk to some social media APIs.
*
* It's ok, the words "social media" make us gag a little too. Have a
* look in SocialNoise/ for useful code.
*/
class SocialNoise {
/**
* Returns an object modelling the search results from the specified
* URL (which is expected to be JSON).
*
* Just a wrapper to avoid cURL and cache boilerplate elsewhere.
*/
public static function getSearchFromJson ($url)
{
$search = function () use ($url) {
// humbly submitted: this is a stupid interface.
$ch = curl_init($url);
curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, \CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
};
// If we've got a memcache wrapper available, store the results
// for a bit.
// TODO: Offer a filesystem-based alternative.
if (class_exists('\SparkCache')) {
$cachekey = get_called_class() . md5($url);
$cache = \SparkCache::getInstance();
return $cache->getOrSet($cachekey, $search);
} else {
return $search();
}
}
}