MT4 Manager gRPC example for PHP
Ready to run project:
https://git.mtapi.io/root/grpc-proto/-/archive/main/grpc-proto-main.zip?path=mt4mng/phpExample
Firstly need to install grpc extensions for PHP.
Linux
sudo pecl install grpc
Add to php.ini:
extension=grpc.so
Windows
Download php_grpc.dll and put it to php/ext folder.
Add to php.ini:
extension=php_grpc.dll
After that you can run following example.
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__.'/GPBMetadata/Mt4Mng.php';
$files = glob(__DIR__.'/Mng4grpc/*.php');
foreach ($files as $file) {
require($file);
}
function toArray($rf)
{
$ret = array();
foreach ($rf as $elem) {
array_push($ret, $elem);
}
return $ret;
}
$service = new Mng4grpc\MainControllerClient('mng4grpc.mtapi.io:443', ['credentials' => Grpc\ChannelCredentials::createSsl()]);
$req = new Mng4grpc\ConnectRequest();
$req->setServer('202.138.248.1');
$req->setUser(10);
$req->setPassword('qwerty11');
list($response, $status) = $service->Connect($req)->wait();
/** @var Mng4grpc\ConnectReply */
$reply = $response;
/** @var Mng4grpc\Error */
$error = $reply->getError();
/** @var string */
$id = $reply->getResult();
if (is_null($error))
print($id . "\n");
else {
print($error . "\n");
print('Press enter to exit...');
fgets(STDIN);
exit();
}
$req = new Mng4grpc\AccountsSummaryRequest();
$req->setId($id);
list($response, $status) = $service->AccountsSummary($req)->wait();
/** @var Mng4grpc\AccountsSummaryReply */
$reply = $response;
/** @var Mng4grpc\Error */
$error = $reply->getError();
/** @var Google\Protobuf\Internal\RepeatedField */
$result = $reply->getResult();
if (is_null($error)) {
foreach ($result as $elem) {
/** @var Mng4grpc\AccountSummary */
$sum = $elem;
print($sum->getLogin().",");
}
print("\n");
}
else {
print($error . "\n");
print('Press enter to exit...');
fgets(STDIN);
exit;
}
print('Press enter to exit...');
fgets(STDIN);