MT4 Manager gRPC example for PHP

Created with Sketch.

MT4 Manager gRPC example for PHP

Table of Contents

Methods browser

MT4 manager gRPC API proto file

Ready to run example

Install gRPC pecl package for Linux.

sudo pecl install grpc

Add to php.ini:

extension=grpc.so

Install PHP gRPC pecl package for Windows

Generate gRPC libraries by composer

Install PHP composer

composer install


The Vendor folder would be created.

Then you need php autogenerated libraries.
You can use them from repository or generate it by yourself.

For Windows

docker run -v ${pwd}:/defs namely/protoc-all -f ./mt4mng.proto -l php -o generated

For Linux

docker run -v PWD:/defs namely/protoc-all -f ./mt4mng.proto -l php -o generated

Add your mt4 server address and credentials before execution.

After that you can run following example.

<?php

require __DIR__ . '/vendor/autoload.php';
require __DIR__.'/GPBMetadata/Mt4MngGrpc/Protos/Mt4Mng.php';
$files = glob(__DIR__.'/Mng4grpc/*.php');
foreach ($files as $file) {
    require($file);
}

$serviceMainClient = new Mng4grpc\MainControllerClient('mng4grpc.mtapi.io:443', ['credentials' => Grpc\ChannelCredentials::createSsl()]);

// TODO ADD YOUR DATA !!!!!
$connectionRequest = new Mng4grpc\ConnectRequest();
$connectionRequest->setServer('');
$connectionRequest->setUser(1);
$connectionRequest->setPassword('');

list($connectionResponse, $status) = $serviceMainClient->Connect($connectionRequest)->wait();
/** @var Mng4grpc\Error $connectionError */
$connectionError = $connectionResponse->getError();
/** @var string $connectionResult */
$connectionResult = $connectionResponse->getResult();
if (is_null($connectionError))
    print($connectionResult . "\n");
else {
    print($connectionError->getMessage() . "\n");
    print('Press enter to exit...');
    fgets(STDIN);
    exit();
}

$accountsSummaryRequest = new Mng4grpc\AccountsSummaryRequest();
$accountsSummaryRequest->setId($connectionResult);

/** @var Mng4grpc\AccountsSummaryReply $accountsSummaryResponse */
list($accountsSummaryResponse, $status) = $serviceMainClient->AccountsSummary($accountsSummaryRequest)->wait();
/** @var Mng4grpc\Error $accountsSummaryError */
$accountsSummaryError = $accountsSummaryResponse->getError();
/** @var Google\Protobuf\Internal\RepeatedField<Mng4grpc\AccountSummary> $accountsSummaryResult */
$accountsSummaryResult = $accountsSummaryResponse->getResult();
if (is_null($accountsSummaryError)) {
    /** @var Mng4grpc\AccountSummary $accountsSummaryResult */
    foreach ($accountsSummaryResult as $accountSummary) {
        print("Login: " . $accountSummary->getLogin() . "\n");
    }
}
else {
    print($accountsSummaryError-> getMessage() . "\n");
    print('Press enter to exit...');
    fgets(STDIN);
    exit;
}

print('Press enter to exit...');
fgets(STDIN);

Leave a Reply

Your email address will not be published. Required fields are marked *