EndpointMysql.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace StatsDBundle\Services;
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. /**
  5. * Read statistics to the statsD
  6. */
  7. class EndpointMysql
  8. {
  9. /**
  10. * @var ContainerInterface
  11. */
  12. protected $serviceContainer;
  13. protected $location;
  14. protected $query;
  15. /**
  16. * @param ContainerInterface $serviceContainer
  17. */
  18. public function __construct(ContainerInterface $serviceContainer)
  19. {
  20. $this->serviceContainer = $serviceContainer;
  21. if($this->serviceContainer->getParameter("statsd.connection")) {
  22. $this->location = $this->serviceContainer->getParameter("statsd.connection");
  23. } else {
  24. $this->location = null;
  25. }
  26. }
  27. /**
  28. * @param string $json
  29. * @param string $query
  30. * query = [last|query|search]
  31. */
  32. public function get($json, $method = "query")
  33. {
  34. if(is_null($this->location)) return array();
  35. $ch = curl_init();
  36. curl_setopt($ch, CURLOPT_URL, "{$this->location}/{$method}");
  37. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  38. curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  39. curl_setopt($ch, CURLOPT_POST, 1);
  40. $headers = array();
  41. $headers[] = "Content-Type: application/json";
  42. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  43. $result = curl_exec($ch);
  44. if (curl_errno($ch)) {
  45. echo 'Error:' . curl_error($ch);
  46. }
  47. curl_close ($ch);
  48. $data = json_decode($result,true);
  49. if(is_array($data))
  50. return $data;
  51. return array();
  52. }
  53. }