{"id":15296183,"url":"https://github.com/yurunsoft/nacos-php","last_synced_at":"2025-04-13T19:31:10.287Z","repository":{"id":43928906,"uuid":"511727103","full_name":"Yurunsoft/nacos-php","owner":"Yurunsoft","description":"Nacos php sdk. Request and response data are all strongly typed and IDE friendly. Complete test cases and support for Swoole Coroutine.","archived":false,"fork":false,"pushed_at":"2024-07-04T10:25:19.000Z","size":98,"stargazers_count":16,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T20:44:11.518Z","etag":null,"topics":["nacos","php","swoole"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Yurunsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2022-07-08T01:43:23.000Z","updated_at":"2025-03-04T07:08:18.000Z","dependencies_parsed_at":"2024-10-15T01:21:19.948Z","dependency_job_id":null,"html_url":"https://github.com/Yurunsoft/nacos-php","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fnacos-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fnacos-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fnacos-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fnacos-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Yurunsoft","download_url":"https://codeload.github.com/Yurunsoft/nacos-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248767866,"owners_count":21158546,"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":["nacos","php","swoole"],"created_at":"2024-09-30T18:09:40.256Z","updated_at":"2025-04-13T19:31:09.986Z","avatar_url":"https://github.com/Yurunsoft.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nacos-php\n\n[![Latest Version](https://img.shields.io/packagist/v/yurunsoft/nacos-php.svg)](https://packagist.org/packages/yurunsoft/nacos-php)\n![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/yurunsoft/nacos-php/test/master)\n[![Php Version](https://img.shields.io/badge/php-%3E=7.4-brightgreen.svg)](https://secure.php.net/)\n[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.0-brightgreen.svg)](https://github.com/swoole/swoole-src)\n[![License](https://img.shields.io/badge/license-Apache2-brightgreen.svg)](https://github.com/yurunsoft/nacos-php/blob/master/LICENSE)\n\n**English** | [中文](README_CN.md)\n\nNacos php sdk for Go client allows you to access Nacos service, it supports service discovery and dynamic configuration.\n\nRequest and response data are all strongly typed and IDE friendly.\n\nComplete test cases and support for Swoole Coroutine.\n\n## Requirements\n\nSupported PHP version over 7.4\n\nSupported Swoole version over 4.4\n\nSupported Nacos version over 1.x\n\n## Installation\n\nUse Composer to install SDK：\n\n`composer require yurunsoft/nacos-php`\n\n## Quick Examples\n\n### Client\n\n```php\nuse Yurun\\Nacos\\Client;\nuse Yurun\\Nacos\\ClientConfig;\n\n// The parameters of ClientConfig are all optional, the ones shown below are the default values\n// You can write only the configuration items you want to modify\n$config = new ClientConfig([\n    'host'                =\u003e '127.0.0.1',\n    'port'                =\u003e 8848,\n    'prefix'              =\u003e '/',\n    'username'            =\u003e '',\n    'password'            =\u003e '',\n    'timeout'             =\u003e 60000, // Network request timeout time, in milliseconds\n    'ssl'                 =\u003e false, // Whether to use ssl(https) requests\n    'authorizationBearer' =\u003e false, // Whether to use the request header Authorization: Bearer {accessToken} to pass Token, older versions of Nacos need to be set to true\n    'maxConnections'      =\u003e 16, // Connection pool max connections\n    'poolWaitTimeout'     =\u003e 30, // Connection pool wait timeout when get connection, in seconds\n    // Custom configuration processor to convert string configuration values to arbitrary types (e.g. arrays), which can override the default processing\n    'configParser'        =\u003e [\n        // type Name =\u003e callback(string value): mixed\n        'json' =\u003e static fn (string $value): array =\u003e json_decode($value, true),\n    ],\n]);\n// Instantiating the client, Not required\n$client = new Client($config);\n\n// Enable log, Support PSR-3\n$logger = new \\Monolog\\Logger();\n$client = new Client($config, $logger);\n\n// Reopening the client, you can execute the first execution in the Swoole worker, process\n$client-\u003ereopen();\n```\n\n### Providers\n\n```php\n// Get provider from client\n\n$auth = $client-\u003eauth;\n$config = $client-\u003econfig;\n$namespace = $client-\u003enamespace;\n$instance = $client-\u003einstance;\n$service = $client-\u003eservice;\n$operator = $client-\u003eoperator;\n```\n\n### Dynamic configuration\n\n#### Get config\n\n```php\n$value = $client-\u003econfig-\u003eget('dataId', 'group');\n```\n\n#### Get parsed config\n\n\u003e Support json, xml, yaml (Required: yaml extension)\n\u003e Nacos \u003e= 1.4\n\n```php\n$client-\u003econfig-\u003eset('dataId', 'group', json_encode(['id' =\u003e 19260817]), 'json');\n$value = $client-\u003econfig-\u003egetParsedConfig('dataId', 'group', '', $type);\n\n// output：\n// array(1) {\n//   [\"id\"]=\u003e\n//   int(19260817)\n// }\nvar_dump($value);\n\nvar_dump($type); // json\n```\n\n#### Set config\n\n```php\n$client-\u003econfig-\u003eset('dataId', 'group', 'value');\n```\n\n#### Delete config\n\n```php\n$client-\u003econfig-\u003edelete('dataId', 'group', 'value');\n```\n\n#### Listen config\n\n#### Config listener\n\n```php\nuse Yurun\\Nacos\\Provider\\Config\\ConfigListener;\nuse Yurun\\Nacos\\Provider\\Config\\Model\\ListenerConfig;\n\n// Get config listener\n$listenerConfig = new ListenerConfig([\n    'timeout'  =\u003e 30000, // The config listener long polling timeout, in milliseconds. The result is returned immediately when the value is 0.\n    'failedWaitTime' =\u003e 3000, // Waiting time to retry after failure, in milliseconds\n    'savePath' =\u003e '', // Config save path, default is empty and not saved to file\n    'fileCacheTime' =\u003e 0, // The file cache time, defaulted to 0, is not affected by caching, and this configuration only affects pull operations.\n]);\n$listener = $client-\u003econfig-\u003egetConfigListener($listenerConfig);\n\n$dataId = 'dataId';\n$groupId = 'groupId';\n$tenant = '';\n\n// Add listening item\n$listener-\u003eaddListener($dataId, $groupId, $tenant);\n// Add listening item with callback\n$listener-\u003eaddListener($dataId, $groupId, $tenant, function (\\ConfigListener $listener, string $dataId, string $group, string $tenant) {\n    // $listener-\u003estop();\n});\n\n// Pull configuration for all listeners (not required)\n// Forced pull, not affected by fileCacheTime\n$listener-\u003epull();\n$listener-\u003epull(true);\n// Pull, affected by fileCacheTime\n$listener-\u003epull(false);\n\n// Manually perform a poll\n$listener-\u003epolling(); // The timeout in the ListenerConfig is used as the timeout time.\n$listener-\u003epolling(30000); // Specify the timeout period\n$listener-\u003epolling(0); // Return results immediately\n\n// Start the polling listener and do not continue with the following statements until you stop\n$listener-\u003estart();\n\n// To get the configuration cache from the listener, you need to call it in another coroutine\n$value = $listener-\u003eget($dataId, $groupId, $tenant, $type);\nvar_dump($type); // Data type\n\n// To get the configuration cache (Arrays or objects after parsing) from the listener, you need to call it in another coroutine\n$value = $listener-\u003egetParsed($dataId, $groupId, $tenant, $type);\nvar_dump($type); // Data type\n```\n\n##### Manual Listening Configuration\n\n\u003e Recommend using the config listener\n\n```php\nuse Yurun\\Nacos\\Provider\\Config\\Model\\ListenerRequest;\n\n$md5 = '';\nwhile (true) {\n    $request = new ListenerRequest();\n    $request-\u003eaddListener('dataId', 'group', $md5);\n    $items = $client-\u003elisten($request);\n    foreach ($items as $item) {\n        if ($item-\u003egetChanged()) {\n            $value = $config-\u003eget($item-\u003egetDataId(), $item-\u003egetGroup(), $item-\u003egetTenant());\n            var_dump('newValue:', $value);\n            $md5 = md5($value);\n        }\n    }\n}\n```\n\n### Service Discovery\n\n#### Register instance\n\n```php\n$client-\u003einstance-\u003eregister('192.168.1.123', 8080, 'Service1');\n// Complete Parameters\n$client-\u003einstance-\u003eregister('192.168.1.123', 8080, 'Service1', $namespaceId = '', $weight = 1, $enabled = true, $healthy = true, $metadata = '', $clusterName = '', $groupName = '', $ephemeral = false);\n```\n\n#### Deregister instance\n\n```php\n$client-\u003einstance-\u003ederegister('192.168.1.123', 8080, 'Service1');\n// Complete Parameters\n$client-\u003einstance-\u003ederegister('192.168.1.123', 8080, 'Service1', $namespaceId = '', $clusterName = '', $groupName = '', $ephemeral = false);\n```\n\n#### Update instance\n\n```php\n$client-\u003einstance-\u003eupdate('192.168.1.123', 8080, 'Service1');\n// Complete Parameters\n$client-\u003einstance-\u003eupdate('192.168.1.123', 8080, 'Service1', $namespaceId = '', $weight = 1, $enabled = true, $healthy = true, $metadata = '', $clusterName = '', $groupName = '', $ephemeral = false);\n```\n\n#### Heartbeat\n\n```php\nuse Yurun\\Nacos\\Provider\\Instance\\Model\\RsInfo;\n\n$beat = new RsInfo();\n$beat-\u003esetIp('192.168.1.123');\n$beat-\u003esetPort(8080);\n$client-\u003einstance-\u003ebeat('Service1', $beat);\n```\n\n#### Get instance list\n\n```php\n$response = $client-\u003einstance-\u003elist('Service1');\n$response = $client-\u003einstance-\u003elist('Service1', $groupName = '', $namespaceId = '', $clusters = '', $healthyOnly = false);\n```\n\n#### Get instance detail\n\n```php\n$response = $client-\u003einstance-\u003edetail('192.168.1.123', 8080, 'Service1');\n// Complete Parameters\n$response = $client-\u003einstance-\u003edetail('192.168.1.123', 8080, 'Service1', $groupName = '', $namespaceId = '', $clusters = '', $healthyOnly = false, $ephemeral = false);\n```\n\n\u003e Other more functional interfaces can be used by referring to the provider object's IDE tips and Nacos documentation.\n\n## Documentation\n\nYou can view the open-api documentation from the [Nacos Open API Guide](https://nacos.io/en-us/docs/open-api.html).\n\nYou can view the full documentation from the [Nacos website](https://nacos.io/en-us/docs/what-is-nacos.html).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyurunsoft%2Fnacos-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyurunsoft%2Fnacos-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyurunsoft%2Fnacos-php/lists"}