EndpointMysql.php 1.8 KB

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