InfluxDB\Client::query PHP Method

query() public method

Query influxDB
public query ( string $database, string $query, array $parameters = [] ) : influxdb\ResultSet
$database string
$query string
$parameters array
return influxdb\ResultSet
    public function query($database, $query, $parameters = [])
    {
        if (!$this->driver instanceof QueryDriverInterface) {
            throw new Exception('The currently configured driver does not support query operations');
        }
        if ($database) {
            $parameters['db'] = $database;
        }
        $driver = $this->getDriver();
        $parameters = ['url' => 'query?' . http_build_query(array_merge(['q' => $query], $parameters)), 'database' => $database, 'method' => 'get'];
        // add authentication to the driver if needed
        if (!empty($this->username) && !empty($this->password)) {
            $parameters += ['auth' => [$this->username, $this->password]];
        }
        $driver->setParameters($parameters);
        try {
            // store the last query sent
            static::$lastQuery = $query;
            // perform the query and return the resultset
            return $driver->query();
        } catch (DriverException $e) {
            throw new Exception('Query has failed', $e->getCode(), $e);
        }
    }

Usage Example

Example #1
0
 /**
  */
 public function testGuzzleQuery()
 {
     $client = new Client('localhost', 8086);
     $query = "some-bad-query";
     $bodyResponse = file_get_contents(dirname(__FILE__) . '/result.example.json');
     $httpMockClient = self::buildHttpMockClient($bodyResponse);
     $client->setDriver(new Guzzle($httpMockClient));
     /** @var \InfluxDB\ResultSet $result */
     $result = $client->query(null, $query);
     $this->assertInstanceOf('\\InfluxDB\\ResultSet', $result);
 }
All Usage Examples Of InfluxDB\Client::query