MT5 manager gRPC PHP8.1 client example
Table of Contents
MT5 manager gRPC methods description
MT5 manager gRPC API proto file
Install gRPC pecl package for Linux.
sudo pecl install grpc
Add to php.ini:
extension=grpc.so
Install PHP gRPC pecl package for Windows
- go to https://pecl.php.net/package/gRPC
- download the package for your operation system and PHP version. For example 8.1 for Windows
- paste it to the PHP extension folder
- update php.ini with this package
extension=grpc
Generate gRPC libraries by 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 ./mng5grpc.proto -l php -o generated
For Linux
docker run -v PWD:/defs namely/protoc-all -f ./mng5grpc.proto -l php -o generated
Add your mt5 server address and credentials before execution.
Now we are ready for execution.
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/GPBMetadata/Mng5Grpc/Protos/Mng5Grpc.php';
$files = glob(__DIR__ . '/Mng5Grpc/*.php');
foreach ($files as $file) {
require($file);
}
$serviceMainClient = new Mng5Grpc\MainClient('mng5grpc.mtapi.io:443', ['credentials' => Grpc\ChannelCredentials::createSsl()]);
// TODO ADD YOUR DATA !!!!!
$connectionRequest = new Mng5Grpc\ConnectRequest();
$connectionRequest -> setServer("");
$connectionRequest -> setPassword("");
$connectionRequest -> setUser(1);
// Connect to the server
list($connectionResponse, $status) = $serviceMainClient->Connect($connectionRequest)->wait();
/** @var Mng5Grpc\Error $connectionError */
$connectionError = $connectionResponse->getError();
/** @var string $connectionResult */
$connectionResult = $connectionResponse->getResult();
if (is_null($connectionError)) {
print($connectionResult);
print("\n");
} else {
print($connectionError->getMessage() . "\n");
print('Press enter to exit...');
fgets(STDIN);
exit;
}
$accountsRequest = new Mng5Grpc\AccountsRequest();
$accountsRequest-> setId($connectionResult);
list($accountsResponse, $status) = $serviceMainClient->AccountsList($accountsRequest)->wait();
/** @var Mng5Grpc\Error $accountsSummaryError */
$accountsSummaryError = $accountsResponse->getError();
/** @var Google\Protobuf\Internal\RepeatedField<int> $accountsSummaryResult */
$accountIdsResult = $accountsResponse->getResult();
if (is_null($accountsSummaryError)) {
/** @var Mng5Grpc\AccountSummary $elem */
foreach ($accountIdsResult as $accountId) {
print("AccountId:" . $accountId ."\n" );
$accountDetailsRequest = new Mng5grpc\AccountDetailsRequest();
$accountDetailsRequest->setId($connectionResult);
$accountDetailsRequest->setLogin($accountId);
list($accountDetailsResponse, $status) = $serviceMainClient->AccountDetails($accountDetailsRequest)->wait();
/** @var Mng5Grpc\Error $accountDetailsError */
$accountDetailsError = $accountDetailsResponse-> getError();
/** @var Mng5Grpc\Account $Account */
$Account = $accountDetailsResponse->getResult();
print("Account balance:" . $Account->getBalance() ."\n");
print("Account profit:" . $Account->getProfit() ."\n");
}
print("\n");
}
else {
/** @var Mng5Grpc\Error $connectionError */
print($accountsSummaryError->getMessage() . "\n");
print('Press enter to exit...');
fgets(STDIN);
exit;
}