Skip to content

Catalog

Daniel Carbone edited this page Nov 6, 2026 · 1 revision

Catalog

$consul->Catalog wraps Consul's Catalog API. This is the cluster-wide view of nodes and services, as opposed to the local view in Agent.

Browsing

[$dcs, $err]         = $consul->Catalog->Datacenters();           // string[]
[$services, $qm, $err] = $consul->Catalog->Services();            // array<string, string[]>: service name => tags
[$nodes, $qm, $err]  = $consul->Catalog->Nodes();                 // Node[]

[$instances, $qm, $err] = $consul->Catalog->Service(service: 'web', tag: 'v1'); // CatalogService[]
foreach ($instances as $s) {
    $addr = '' !== $s->ServiceAddress ? $s->ServiceAddress : $s->Address;
    echo "{$s->ServiceID} {$addr}:{$s->ServicePort}\n";
}

[$node, $qm, $err] = $consul->Catalog->Node(node: 'my-node');     // CatalogNode: ->Node, ->Services

As in Consul, ServiceAddress is empty when the service uses its node's address.

Use Health instead of Catalog->Service if you only want healthy instances.

Registering external services

use DCarbone\PHPConsulAPI\Agent\AgentService;
use DCarbone\PHPConsulAPI\Catalog\CatalogDeregistration;
use DCarbone\PHPConsulAPI\Catalog\CatalogRegistration;

[$wm, $err] = $consul->Catalog->Register(catalogRegistration: new CatalogRegistration(
    Node: 'external-db',
    Address: '10.0.0.50',
    Service: new AgentService(ID: 'postgres-1', Service: 'postgres', Port: 5432),
));

[$wm, $err] = $consul->Catalog->Deregister(catalogDeregistration: new CatalogDeregistration(Node: 'external-db'));

See Responses and Errors and Query and Write Options.

Clone this wiki locally