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.

83 lines
1.6 KiB

  1. <?php
  2. namespace SparkLib;
  3. /**
  4. * An interface to Phant instances.
  5. */
  6. class Phant {
  7. protected $_endpoint = 'http://data.sparkfun.com';
  8. protected $_pubhash;
  9. protected $_privhash;
  10. public function __construct ($endpoint, $pubhash, $privhash)
  11. {
  12. $this->_endpoint = $endpoint;
  13. $this->_pubhash = $pubhash;
  14. $this->_privhash = $privhash;
  15. }
  16. /**
  17. * Write data to a stream for which we know the private hash.
  18. */
  19. public function input (array $data)
  20. {
  21. $postbody = http_build_query($data);
  22. $headers = [
  23. "Phant-Private-Key: {$this->_privhash}",
  24. "Content-type: application/x-www-form-urlencoded",
  25. ];
  26. $opts = [
  27. 'http' => [
  28. 'method' => 'POST',
  29. 'header' => $headers,
  30. 'content' => $postbody
  31. ]
  32. ];
  33. $url = $this->_endpoint . '/input/' . $this->_pubhash . '.txt';
  34. $context = stream_context_create($opts);
  35. return file_get_contents($url, false, $context);
  36. }
  37. /**
  38. * Get current stats for the stream.
  39. */
  40. public function stats ()
  41. {
  42. $opts = [
  43. 'http' => [
  44. 'method' => 'GET',
  45. ]
  46. ];
  47. $url = $this->_endpoint . '/output/' . $this->_pubhash . '/stats';
  48. $context = stream_context_create($opts);
  49. return json_decode(file_get_contents($url, false, $context));
  50. }
  51. /**
  52. * Return stream data.
  53. */
  54. public function data ()
  55. {
  56. $opts = [
  57. 'http' => [
  58. 'method' => 'GET',
  59. ]
  60. ];
  61. $url = $this->_endpoint . '/output/' . $this->_pubhash . '.json';
  62. $context = stream_context_create($opts);
  63. return json_decode(file_get_contents($url, false, $context));
  64. }
  65. }