PHP code example of muqiuren / nacos-php

1. Go to this page and download the library: Download muqiuren/nacos-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

muqiuren / nacos-php example snippets


use Hatch\Nacos\NacosClient;

$options = [
    // nacos服务端地址
    'host' => '127.0.0.1',
    // nacos服务端端口
    'port' => 8848,
    // 命名空间id
    'namespace_id' => '1e7b3de6-7edb-4329-9184-46582480063b',
    // 配置id
    'data_id' => 'php_env_config',
    // 配置组
    'group' => 'test',
    // nacos用户名
    'username' => 'admin',
    // nacos密码
    'password' => 'admin',
];

$client = new NacosClient($options);
// 获取配置
$conf = $client->configs->get();
var_dump(conf);

use Hatch\Nacos\NacosClient;

$options = [
    // nacos服务端地址
    'host' => '127.0.0.1',
    // nacos服务端端口
    'port' => 8848,
    // 命名空间id
    'namespace_id' => '1e7b3de6-7edb-4329-9184-46582480063b',
    // 配置id
    'data_id' => 'php_env_config',
    // 配置组
    'group' => 'test',
    // nacos用户名
    'username' => 'admin',
    // nacos密码
    'password' => 'admin',
    // 自动保存的文件地址
    'save_config_path' => '.env',
];

$client = new NacosClient($options);
// 启动监听,会阻塞当前进程
$client->configs->listen(function($newMd5) {
    var_dump($newMd5);
});
powershell
composer