{"id":18256108,"url":"https://github.com/friendsofphp/consul-php-sdk","last_synced_at":"2025-05-15T18:06:32.499Z","repository":{"id":23395205,"uuid":"26757079","full_name":"FriendsOfPHP/consul-php-sdk","owner":"FriendsOfPHP","description":"PHP Consul SDK","archived":false,"fork":false,"pushed_at":"2024-03-04T08:05:08.000Z","size":31329,"stargazers_count":317,"open_issues_count":0,"forks_count":55,"subscribers_count":17,"default_branch":"main","last_synced_at":"2025-04-22T22:41:33.468Z","etag":null,"topics":["consul","consul-sdk","php","sdk"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FriendsOfPHP.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-11-17T13:10:57.000Z","updated_at":"2025-01-25T10:25:37.000Z","dependencies_parsed_at":"2024-06-16T07:42:29.617Z","dependency_job_id":null,"html_url":"https://github.com/FriendsOfPHP/consul-php-sdk","commit_stats":{"total_commits":68,"total_committers":20,"mean_commits":3.4,"dds":0.3970588235294118,"last_synced_commit":"8ae4e60b48ae1abe2d235838db290d0b7e5dcfa2"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FriendsOfPHP%2Fconsul-php-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FriendsOfPHP%2Fconsul-php-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FriendsOfPHP%2Fconsul-php-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FriendsOfPHP%2Fconsul-php-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FriendsOfPHP","download_url":"https://codeload.github.com/FriendsOfPHP/consul-php-sdk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254394719,"owners_count":22063984,"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":["consul","consul-sdk","php","sdk"],"created_at":"2024-11-05T10:20:00.136Z","updated_at":"2025-05-15T18:06:32.474Z","avatar_url":"https://github.com/FriendsOfPHP.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Consul PHP SDK\n\nConsul PHP SDK is a thin wrapper around the [Consul](https://consul.io/) HTTP API.\n\n## Compatibility\n\nSee previous version of\n[README.md](https://github.com/FriendsOfPHP/consul-php-sdk/tree/404366acbce4285d08126c0a55ace84c10e361d1)\nto find some version compatible with older version of symfony/http-client or\nguzzle\n\n## Installation\n\nThis library can be installed with composer:\n\n    composer require friendsofphp/consul-php-sdk\n\n## Supported services\n\n* agent\n* catalog\n* health\n* kv\n* session\n* txn\n\n## Usage\n\nInstantiate a services, and start using it:\n\n```php\n\n$kv = new Consul\\Services\\KV();\n\n$kv-\u003eput('test/foo/bar', 'bazinga');\n$kv-\u003eget('test/foo/bar', ['raw' =\u003e true]);\n$kv-\u003edelete('test/foo/bar');\n```\n\nA service exposes few methods mapped from the consul [API](https://consul.io/docs/agent/http.html):\n\n**All services methods follow the same convention:**\n\n```php\n$response = $service-\u003emethod($mandatoryArgument, $someOptions);\n```\n\n* All API mandatory arguments are placed as first;\n* All API optional arguments are directly mapped from `$someOptions`;\n* All methods return a `Consul\\ConsulResponse`;\n* If the API responds with a 4xx response, a `Consul\\Exception\\ClientException` is thrown;\n* If the API responds with a 5xx response, a `Consul\\Exception\\ServeException` is thrown.\n\n## Cookbook\n\n### How to acquire an exclusive lock?\n\n```php\n$session = new Consul\\Services\\Session();\n\n$sessionId = $session-\u003ecreate()-\u003ejson()['ID'];\n\n// Lock a key / value with the current session\n$lockAcquired = $kv-\u003eput('tests/session/a-lock', 'a value', ['acquire' =\u003e $sessionId])-\u003ejson();\n\nif (false === $lockAcquired) {\n    $session-\u003edestroy($sessionId);\n\n    echo \"The lock is already acquire by another node.\\n\";\n    exit(1);\n}\n\necho \"Do you jobs here....\";\nsleep(5);\necho \"End\\n\";\n\n$kv-\u003edelete('tests/session/a-lock');\n$session-\u003edestroy($sessionId);\n```\n\n### How to use MultiLockHandler?\n\n```php\n$resources = ['resource1', 'resource2'];\n\n$multiLockHandler = new MultiLockHandler($resources, 60, new Session(), new KV(), 'my/lock/');\n\nif ($multiLockHandler-\u003elock()) {\n    try {\n        echo \"Do you jobs here....\";\n    } finally {\n        $multiLockHandler-\u003erelease();    \n    }\n}\n```\n\n\n### How to use MultiSemaphore?\n\n```php\n$resources = [\n    new Resource('resource1', 2, 7),\n    new Resource('resource2', 3, 6),\n    new Resource('resource3', 1, 1),\n];\n\n$semaphore = new MultiSemaphore($resources, 60, new Session(), new KV(), 'my/semaphore');\n\nif ($semaphore-\u003eacquire()) {\n    try {\n        echo \"Do you jobs here....\";\n    } finally {\n        $semaphore-\u003erelease();    \n    }\n}\n```\n\n## Some utilities\n\n* `Consul\\Helper\\LockHandler`: Simple class that implement a distributed lock\n* `Consul\\Helper\\MultiLockHandler`: Simple class that implements a distributed lock for many resources\n* `Consul\\Helper\\MultiSemaphore`: Simple class that implements a distributed semaphore for many resources\n\n## Run the test suite\n\nYou need a consul agent running on `localhost:8500`.\n\nBut you ca override this address:\n\n```\nexport CONSUL_HTTP_ADDR=172.17.0.2:8500\n```\n\nIf you don't want to install Consul locally you can use a docker container:\n\n```\ndocker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 consul\n```\n\nThen, run the test suite\n\n```\nvendor/bin/simple-phpunit\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriendsofphp%2Fconsul-php-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffriendsofphp%2Fconsul-php-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffriendsofphp%2Fconsul-php-sdk/lists"}