{"id":13529461,"url":"https://github.com/corley/influxdb-php-sdk","last_synced_at":"2025-04-01T16:31:28.861Z","repository":{"id":20588962,"uuid":"23869671","full_name":"corley/influxdb-php-sdk","owner":"corley","description":"InfluxDB PHP SDK - UDP/IP or HTTP adapters for read and write data","archived":false,"fork":false,"pushed_at":"2019-06-03T02:30:45.000Z","size":283,"stargazers_count":84,"open_issues_count":3,"forks_count":23,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-16T19:18:24.016Z","etag":null,"topics":["chronograf","influx","influx-data","influxdb","influxdb-php-sdk","iot","kapacitor","php","query-builder","sdk","telegraf","udp"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"PetterKraabol/laravel-twitch-api","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/corley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-09-10T10:01:26.000Z","updated_at":"2023-09-15T08:12:38.000Z","dependencies_parsed_at":"2022-09-05T16:51:47.473Z","dependency_job_id":null,"html_url":"https://github.com/corley/influxdb-php-sdk","commit_stats":null,"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/corley%2Finfluxdb-php-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/corley%2Finfluxdb-php-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/corley%2Finfluxdb-php-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/corley%2Finfluxdb-php-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/corley","download_url":"https://codeload.github.com/corley/influxdb-php-sdk/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246670544,"owners_count":20815003,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chronograf","influx","influx-data","influxdb","influxdb-php-sdk","iot","kapacitor","php","query-builder","sdk","telegraf","udp"],"created_at":"2024-08-01T07:00:36.549Z","updated_at":"2025-04-01T16:31:28.461Z","avatar_url":"https://github.com/corley.png","language":"PHP","readme":"# InfluxDB PHP SDK\n\n[![Build Status](https://travis-ci.org/corley/influxdb-php-sdk.svg?branch=master)](https://travis-ci.org/corley/influxdb-php-sdk)\n[![Code Coverage](https://scrutinizer-ci.com/g/corley/influxdb-php-sdk/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/corley/influxdb-php-sdk/?branch=master)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/corley/influxdb-php-sdk/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/corley/influxdb-php-sdk/?branch=master)\n[![Latest Stable Version](https://poser.pugx.org/corley/influxdb-sdk/v/stable)](https://packagist.org/packages/corley/influxdb-sdk)\n[![License](https://poser.pugx.org/corley/influxdb-sdk/license)](https://packagist.org/packages/corley/influxdb-sdk)\n\nSend metrics to InfluxDB and query for any data.\n\nThis project support InfluxDB API `\u003e= 0.9` - **For InfluxDB v0.8 checkout branch 0.3 (no longer supported)**\n\nSupported adapters:\n\n * UDP/IP\n * HTTP (via GuzzleHTTP versions: ~5, ~6) - **For Guzzle 4 support checkout branch 0.9 (no longer supported)**\n\n## Install it\n\nJust use composer\n\n```sh\n$ composer require corley/influxdb-sdk:~1\n```\n\nOr add to your `composer.json` file\n\n```json\n{\n  \"require\": {\n    \"corley/influxdb-sdk\": \"~1\"\n  }\n}\n```\n\n## Use it\n\nAdd new points:\n\n```php\n$client-\u003emark(\"app-search\", [\n    \"key\" =\u003e \"this is my search\"\n]);\n```\n\nOr use InfluxDB direct messages\n\n```php\n$client-\u003emark([\n    \"tags\" =\u003e [\n        \"dc\" =\u003e \"eu-west-1\",\n    ],\n    \"points\" =\u003e [\n        [\n            \"measurement\" =\u003e \"instance\",\n            \"fields\" =\u003e [\n                \"cpu\" =\u003e 18.12,\n                \"free\" =\u003e 712423,\n            ],\n        ],\n    ]\n]);\n```\n\nRetrieve existing points:\n\n```php\n$results = $client-\u003equery('select * from \"app-search\"');\n```\n\n## InfluxDB client adapters\n\nActually we supports two network adapters\n\n * UDP/IP - in order to send data via UDP/IP (datagram)\n * HTTP - in order to send/retrieve using HTTP messages (connection oriented)\n\n### Using UDP/IP Adapter\n\nIn order to use the UDP/IP adapter your must have PHP compiled with the `sockets` extension.\n\n**Usage**\n\n```php\n$reader = ...\n\n$options = new Udp\\Options();\n$writer = new Udp\\Writer($options);\n\n$client = new Client($reader, $writer);\n```\n\n_UDP/IP option set have only `host` and `port` properties you configure\ndatabase and retention policies directly in your InfluxDB configuration_\n\n### Using HTTP Adapters\n\nActually Guzzle is used as HTTP client library\n\n```php\n\u003c?php\n$http = new \\GuzzleHttp\\Client();\n\n$writer = ...\n\n$options = new Http\\Options();\n$reader = new Http\\Reader($http, $options);\n\n$client = new Client($reader, $writer);\n```\n\n## Mixing readers and writers\n\nOf course you can mix Udp\\Ip and Http adapters in order to write data points\nwith UDP/IP protocol but read information using HTTP.\n\n```php\n$reader = new Http\\Reader($http, $httpOptions);\n$writer = new Udp\\Writer($udpOptions);\n$client = new Client($reader, $writer);\n\n$client-\u003emark(...); // Use UDP/IP support\n$client-\u003equery(\"SELECT * FROM my_serie\"); // Use HTTP support\n```\n\nOr use only the HTTP\n\n```php\n$reader = new Http\\Reader($http, $options);\n$writer = new Http\\Writer($http, $options);\n$client = new Client($reader, $writer);\n\n$client-\u003emark(...); // Use HTTP support\n$client-\u003equery(\"SELECT * FROM my_serie\"); // Use HTTP support\n```\n\n### Query InfluxDB\n\nYou can query the time series database using the query method.\n\n```php\n$client-\u003equery('select * from \"mine\"');\n```\n\nThe adapter returns the json decoded body of the InfluxDB response, something\nlike:\n\n```\narray(1) {\n  'results' =\u003e\n  array(1) {\n    [0] =\u003e\n    array(1) {\n      'series' =\u003e\n      array(1) {\n        ...\n      }\n    }\n  }\n}\n```\n\nIf you prefere a more simple response than the original one, you can use\n[`corley/influxdb-http-handlers`](https://github.com/corley/influxdb-http-handlers)\nthat convert, the original InfluxDB response, in a more simple response, something like:\n\n```php\narray(1) {\n  'serie_name' =\u003e array(2) {\n    [0] =\u003e array(4) {\n      'time' =\u003e string(30) \"2015-09-09T20:42:07.927267636Z\"\n      'value1' =\u003e int(1)\n      'value2' =\u003e int(2)\n      'valueS' =\u003e string(6) \"string\"\n    }\n    [1] =\u003e array(4) {\n      'time' =\u003e string(30) \"2015-09-09T20:42:51.332853369Z\"\n      'value1' =\u003e int(2)\n      'value2' =\u003e int(4)\n      'valueS' =\u003e string(11) \"another-one\"\n    }\n  }\n}\n```\n\n## Global tags and retention policy\n\nYou can set a set of default tags, that the SDK will add to your metrics:\n\n```php\n$options = new Http\\Options();\n$options-\u003esetTags([\n    \"env\" =\u003e \"prod\",\n    \"region\" =\u003e \"eu-west-1\",\n]);\n```\n\nThe SDK mark all point adding those tags.\n\nYou can set a default retentionPolicy using\n\n```\n$options-\u003esetRetentionPolicy(\"myPolicy\");\n```\n\nIn that way the SDK use that policy instead of `default` policy.\n\n## Proxies and InfluxDB\n\nIf you proxy your InfluxDB typically you have a prefix in your endpoints.\n\n```php\n$option-\u003esetHost(\"proxy.influxdb.tld\");\n$option-\u003esetPort(80);\n$option-\u003esetPrefix(\"/influxdb\"); // your prefix is: /influxdb\n\n// final url will be: http://proxy.influxdb.tld:80/influxdb/write\n\n$client-\u003emark(\"serie\", [\"data\" =\u003e \"my-data\"]);\n```\n\n## Data type management\n\nFrom InfluxDB version `\u003e=0.9.3` integer types are marked with a trailing `i`.\nThis library supports data types, in particular PHP types are mapped to influxdb\nin this way by defaults:\n\nAnd the resulting mapping will be:\n\n| PHP     | InfluxDB |\n|---------|----------|\n| int     | int64    |\n| double  | float64  |\n| boolean | boolean  |\n| string  | string   |\n\n```php\n$client-\u003emark(\"serie\", [\n    \"value\" =\u003e 12,  // Marked as int64\n    \"elem\" =\u003e 12.4, // Marked as float64\n]);\n```\n\n### Force data type\n\nIf you want to ensure that a type is effectively parsed correctly you can force it directly during the send operation\n\n```php\n$client-\u003emark(\"serie\", [\n    \"value\"  =\u003e new IntType(12),  // Marked as int64\n    \"elem\"   =\u003e new FloatType(12.4), // Marked as float64\n    \"status\" =\u003e new BoolType(true), // Marked as boolean\n    \"line\"   =\u003e new StringType(\"12w\"), // Marked as string\n]);\n```\n\n### Query Builder\n\nInterested in a Query Builder?\n\nhttps://github.com/corley/dbal-influxdb\n\nThanks to Doctrine DBAL (Abstract Layer) you can use the query builder\n\n```php\n$qb = $conn-\u003ecreateQueryBuilder();\n\n$qb-\u003eselect(\"*\")\n    -\u003efrom(\"cpu_load_short\")\n    -\u003ewhere(\"time = ?\")\n    -\u003esetParameter(0, 1434055562000000000);\n\n$data = $qb-\u003eexecute();\nforeach ($data-\u003efetchAll() as $element) {\n    // Use your element\n}\n```\n\n```php\n$config = new \\Doctrine\\DBAL\\Configuration();\n//..\n$connectionParams = array(\n    'dbname' =\u003e 'mydb',\n    'user' =\u003e 'root',\n    'password' =\u003e 'root',\n    'host' =\u003e 'localhost',\n    'port' =\u003e 8086,\n    \"driverClass\" =\u003e \"Corley\\\\DBAL\\\\Driver\\\\InfluxDB\",\n);\n$conn = \\Doctrine\\DBAL\\DriverManager::getConnection($connectionParams, $config);\n```\n\n## Database operations and custom queries\n\nThe class `InfluxDB\\Client` does not support any database operation by itself.\nThat means that you don't have any helper function for create new databases, or\nlist actual databases and so on.\nThanks to `InfluxDB\\Manager` you can use a preset of queries and create your own\npersonal queries that will run against the Influxdb instance.\n\n### Create the manager\n\nThe `Manager` instance is very simple, you have to create it with a client\ninstance.\n\n```php\n$manager = new Manager($client); // InfluxDB\\Client instance\n```\n\n### Attach new queries\n\nThe manager allows to attach new queries via an helper method `addQuery`.\n\n```php\n$manager-\u003eaddQuery(\"getExceptionsInMinutes\", function($minutes) {\n    return \"SELECT * FROM app_exceptions WHERE time \u003e now() - {$minutes}m\";\n});\n\n$manager-\u003egetExceptionsInMinutes(10); // The callable name\n```\n\nAs you can see you have to label your anonymous function and reuse it via the\nmanager.\n\nIn order to collect and reuse custom queries you can define query objects:\n\n```php\nclass GetExceptionsInMinutes\n{\n    public function __invoke($minutes)\n    {\n        return \"SELECT * FROM app_exceptions WHERE time \u003e now() - {$minutes}m\";\n    }\n\n    public function __toString()\n    {\n        return \"getExceptionsInMinutes\";\n    }\n}\n\n$manager-\u003eaddQuery(new GetExceptionsInMinutes());\n\n$manager-\u003egetExceptionsInMinutes(10); //Use the query\n```\n\nAs you can see valid query command should be `callable` via the `__invoke`\nmethod and should be also serializable as strings via `__toString` method\n\n### Existing queries\n\nThis project comes out with a preset of valid queries:\n\n * Create new database `InfluxDB\\Query\\CreateDatabase`\n * Drop existing databases `InfluxDB\\Query\\DeleteDatabase`\n * List existing databases `InfluxDB\\Query\\GetDatabases`\n\n```php\n$manager-\u003eaddQuery(new CreateDatabase());\n$manager-\u003eaddQuery(new DeleteDatabase());\n$manager-\u003eaddQuery(new GetDatabases());\n```\n\n## FAQ\n\n### Add sockets support to your PHP\n\nTo verify if you have the `sockets` extension just issue a:\n\n```bash\nphp -m | grep sockets\n```\n\nIf you don't have the `sockets` extension, you can proceed in two ways:\n\n  - Recompile your PHP whith the `--enable-sockets` flag\n  - Or just compile the `sockets` extension extracting it from the PHP source.\n\n  1. Download the source relative to the PHP version that you on from [here](https://github.com/php/php-src/releases)\n  2. Enter in the `ext/sockets` directory\n  3. Issue a `phpize \u0026\u0026 ./configure \u0026\u0026 make -j \u0026\u0026 sudo make install`\n  4. Add `extension=sockets.so` to your php.ini\n\n### Guzzle 4 support\n\nWe drop the Guzzle 4 support, but we tested it as working HTTP adapter in up\nto PHP 7.0 and is stable with this project with version 0.9.3.\n\nIf you need Guzzle 4 as HTTP adapter require for 0.9.3 version\n\n```sh\ncompose require corley/influxdb-sdk:0.9.*\n```\n\n","funding_links":[],"categories":["Client libraries"],"sub_categories":["Unofficial"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorley%2Finfluxdb-php-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcorley%2Finfluxdb-php-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorley%2Finfluxdb-php-sdk/lists"}