PHP code example of alex-qiu / dubbo-php-client

1. Go to this page and download the library: Download alex-qiu/dubbo-php-client 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/ */

    

alex-qiu / dubbo-php-client example snippets



use DubboPhp\Client\Client;

$options = [
    'registry_address' => '127.0.0.1:2181',
    'version' => '1.0.0',
    'group' =>null,
    'protocol' => 'jsonrpc'
];

try {
    $dubboCli = new Client($options);
    $testService = $dubboCli->getService("com.dubbo.demo.HelloService");
    $ret = $testService->hello("dubbo php client");
    var_dump($ret);
    $mapRet = $testService->mapEcho();
    var_dump($mapRet);

    $objectRet = $testService->objectEcho();
    var_dump($objectRet);

    /**
     * getService method support 2 way. If the forceVgp = true, It will assign the function parameter to service version,group and protocol. Default way is assign the $options configs to these.
     * getService支持两种方式调用。如果forceVgp=true, 该方法将使用传参来绑定服务的版本号,组和协议。默认方式是使用$options数组里的配置绑定。
     */
    $testServiceWithvgp = $dubboCli->getService("com.dubbo.demo.HelloService","1.0.0",null, $forceVgp = true);
    $vgpRet = $testServiceWithvgp->hello("this request from vgp");
    var_dump($vgpRet);
} catch (\DubboPhp\Client\DubboPhpException $e) {
    print($e->getMessage());
}



use DubboPhp\Client\Client;

$options = [
    'registry_address' => '127.0.0.1:2181',
    'version' => '1.0.0',
    'group' =>null,
    'protocol' => 'jsonrpc'
];

try {
    $dubboCli = new Client($options);
    $testService = $dubboCli->getService("com.dubbo.demo.HelloService");
    $ret = $testService->hello("dubbo php client");
    var_dump($ret);
    $mapRet = $testService->mapEcho();
    var_dump($mapRet);

    $objectRet = $testService->objectEcho();
    var_dump($objectRet);

    /**
     * getService method support 2 way. If the forceVgp = true, It will assign the function parameter to service version,group and protocol. Default way is assign the $options configs to these.
     * getService支持两种方式调用。如果forceVgp=true, 该方法将使用传参来绑定服务的版本号,组和协议。默认方式是使用$options数组里的配置绑定。
     */
    $testServiceWithvgp = $dubboCli->getService("com.dubbo.demo.HelloService","1.0.0",null, $forceVgp = true);
    $vgpRet = $testServiceWithvgp->hello("this request from vgp");
    var_dump($vgpRet);
} catch (\DubboPhp\Client\DubboPhpException $e) {
    print($e->getMessage());
}


DubboPhp\Client\Providers\DubboServiceProvider::class


'DubboPhpClient'=>DubboPhp\Client\Facades\DubboPhpClient::class,

'DubboPhpClientFactory'=>DubboPhp\Client\Facades\DubboPhpClientFactory::class,


php artisan vendor:publish --provider="DubboPhp\Client\DubboPhpClientServiceProvider"


$testService = DubboPhpClient::getService('com.dubbo.demo.HelloService');

$ret = $testService->hello("dubbo php client");
var_dump($ret);
    

$clientA = DubboPhpClientFactory::factory(config('dubbo_cli.connections.xxxA'));
$testServiceA = $clientA->getService('com.dubbo.demo.HelloService');
$retA = $testServiceA->hello("dubbo php client");
var_dump($retA);

$clientB = DubboPhpClientFactory::factory(config('dubbo_cli.connections.xxxB'));
$testServiceB = $clientB->getService('com.dubbo.demo.HelloService');
$retB = $testServiceB->hello("dubbo php client");
var_dump($retB);