|
@@ -0,0 +1,67 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace StatsDBundle\Services;
|
|
|
+
|
|
|
+use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Read statistics to the statsD
|
|
|
+ */
|
|
|
+class EndpointMysql
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * @var ContainerInterface
|
|
|
+ */
|
|
|
+ protected $serviceContainer;
|
|
|
+ protected $location;
|
|
|
+ protected $query;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param ContainerInterface $serviceContainer
|
|
|
+ */
|
|
|
+ public function __construct(ContainerInterface $serviceContainer)
|
|
|
+ {
|
|
|
+ $this->serviceContainer = $serviceContainer;
|
|
|
+
|
|
|
+ if($this->serviceContainer->getParameter("statsd.connection")) {
|
|
|
+ $this->location = $this->serviceContainer->getParameter("statsd.connection");
|
|
|
+ } else {
|
|
|
+ $this->location = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $json
|
|
|
+ * @param string $query
|
|
|
+ * query = [last|query|search]
|
|
|
+ */
|
|
|
+
|
|
|
+ public function get($json, $method = "query")
|
|
|
+ {
|
|
|
+ if(is_null($this->location)) return array();
|
|
|
+
|
|
|
+ $ch = curl_init();
|
|
|
+
|
|
|
+ curl_setopt($ch, CURLOPT_URL, "{$this->location}/{$method}");
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
|
|
|
+ curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
+
|
|
|
+ $headers = array();
|
|
|
+ $headers[] = "Content-Type: application/json";
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
+
|
|
|
+ $result = curl_exec($ch);
|
|
|
+ if (curl_errno($ch)) {
|
|
|
+ echo 'Error:' . curl_error($ch);
|
|
|
+ }
|
|
|
+ curl_close ($ch);
|
|
|
+
|
|
|
+ $data = json_decode($result,true);
|
|
|
+ if(is_array($data))
|
|
|
+ return $data;
|
|
|
+
|
|
|
+ return array();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|