PHP client for the Consul HTTP API
This library is loosely based upon the official GO client.
PHPConsulAPI Version | Consul Version |
---|---|
0.3.x | 0.6.4 |
0.6.x | 0.7-0.8 |
dev-master | 0.9-1.0.0 |
Newer versions of the api lib will probably work in a limited capacity with older versions of Consul, but no guarantee is made and backwards compatibility issues will not be addressed.
This lib is designed to be used with Composer
Require Entry:
{
"require": {
"dcarbone/php-consul-api": "@stable"
}
}
First, construct a Config. This class is modeled quite closely after the Config Struct present in the Consul API Subpackage.
If you have defined some of the Consul Environment Variables on your hosts then it would probably be easiest to simply execute the following:
$config = \DCarbone\PHPConsulAPI\Config::newDefaultConfig();
You may alternatively define values yourself:
$config = new \DCarbone\PHPConsulAPI\Config([
'HttpClient' => $client, // [required] Client conforming to GuzzleHttp\ClientInterface
'Address' => 'address of server', // [required]
'Scheme' => 'http or https', // [optional] defaults to "http"
'Datacenter' => 'name of datacenter', // [optional]
'HttpAuth' => 'user:pass', // [optional]
'Token' => 'auth token', // [optional] default auth token to use
'TokenInHeader' => true, // [optional] specifies whether to send the token in the header or query string
'InsecureSkipVerify' => false, // [optional] if set to true, ignores all SSL validation
'CAFile' => '', // [optional] path to ca cert file, see http://docs.guzzlephp.org/en/latest/request-options.html#verify
'CertFile' => '', // [optional] path to client public key. if set, requires KeyFile also be set
'KeyFile' => '', // [optional] path to client private key. if set, requires CertFile also be set
]);
By default, this client will attempt to locate a series of environment variables to describe much of the above configuration properties. See here for that list, and see here for a list of the env var names.
For more advanced client configuration, such as proxy configuration, you must construct your own GuzzleHttp client prior to constructing a PHPConsulAPI Config object.
As an example:
$proxyClient = new \GuzzleHttp\Client(['proxy' => 'whatever proxy you want']]);
$config = new \DCarbone\PHPConsulAPI\Config([
'HttpClient' => $proxyClient,
'Address' => 'address of server',
]);
When constructing your client, if you are using the GuzzleHttp\Client
object directly or derivative thereof, you may
pass any option listed in the Guzzle Request Options.
Next, construct a Consul object:
$consul = new \DCarbone\PHPConsulAPI\Consul($config);
NOTE: If you do not create your own config object, Consul will create it's own using Config::newDefaultConfig() and attempt to locate a suitable HTTP Client.
Once constructed, you interact with each Consul API via it's corresponding Client class:
list($kv_list, $qm, $err) = $consul->KV->keys();
if (null !== $err)
die($err);
var_dump($kv_list);
...as an example.
More will be added as time goes on!
The testing suite is still in it's infancy, however it is being tested directly against an actual Consul agent. They will be back-filled as time allows. Future plans are to set up a simple cluster to provide a more real-world testing scenario.