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

  1. <?php
  2. namespace SparkLib;
  3. /**
  4. * A base class for simple wrappers to talk to some social media APIs.
  5. *
  6. * It's ok, the words "social media" make us gag a little too. Have a
  7. * look in SocialNoise/ for useful code.
  8. */
  9. class SocialNoise {
  10. /**
  11. * Returns an object modelling the search results from the specified
  12. * URL (which is expected to be JSON).
  13. *
  14. * Just a wrapper to avoid cURL and cache boilerplate elsewhere.
  15. */
  16. public static function getSearchFromJson ($url)
  17. {
  18. $search = function () use ($url) {
  19. // humbly submitted: this is a stupid interface.
  20. $ch = curl_init($url);
  21. curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1);
  22. curl_setopt($ch, \CURLOPT_HEADER, 0);
  23. $result = curl_exec($ch);
  24. curl_close($ch);
  25. return json_decode($result);
  26. };
  27. // If we've got a memcache wrapper available, store the results
  28. // for a bit.
  29. // TODO: Offer a filesystem-based alternative.
  30. if (class_exists('\SparkCache')) {
  31. $cachekey = get_called_class() . md5($url);
  32. $cache = \SparkCache::getInstance();
  33. return $cache->getOrSet($cachekey, $search);
  34. } else {
  35. return $search();
  36. }
  37. }
  38. }