12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace StatsDBundle\Services;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use \Exception;
- /**
- * Read statistics to the statsD
- */
- class EndpointMongo
- {
- /**
- * @var ContainerInterface
- */
- protected $serviceContainer;
- protected $location;
- protected $query;
- /**
- * @param ContainerInterface $serviceContainer
- */
- public function __construct(ContainerInterface $serviceContainer)
- {
- $this->serviceContainer = $serviceContainer;
- if($this->serviceContainer->getParameter("endpoint.mongodb.connection")) {
- $this->location = $this->serviceContainer->getParameter("endpoint.mongodb.connection");
- } else {
- $this->location = null;
- }
- }
- /**
- * @param string $json
- * @param string $query
- * query = [last|query|search]
- */
-
- public function get($json, $method = "query", $exception = false)
- {
- 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)) {
- if($exception) {
- curl_close($ch);
- throw new Exception(curl_error($ch));
- } else {
- echo 'Error:' . curl_error($ch);
- }
- }
- curl_close($ch);
- $data = json_decode($result,true);
- if(is_array($data))
- return $data;
- return array();
- }
- }
|