MT4 gRPC PHP8.1 client example
Table of Contents
Install PHP.
For windows you need version 8.1, because, there isn't compiled grpc library for 8.2.
Install grpc pecl package for Linux.
sudo pecl install grpc
Add to php.ini:
extension=grpc.so
Install grpc pecl package for Windows.
- go to https://pecl.php.net/package/gRPC
- and download the package for your operation system. For example for Windows
- paste it to the PHP extension folder
- and update php.ini with this package
extension=grpc
Install composer package.
You should have composer json config file.{ "require": { "google/protobuf": "^3.23.3", "grpc/grpc": "^1.52.0" } }
Then, we need to install packages, according to the config file.
composer install
Then we need to generate some php libraries by proto file.
Check you have proto file mt4.proto in the solution folder.
And generate files by docker.
For windows:
docker run -v ${pwd}:/defs namely/protoc-all -f ./mt4.proto -l php -o generated
For linux:
docker run -v PWD:/defs namely/protoc-all -f ./mt4.proto -l php -o generated
You might find generated libraries here.
Files were generated
Now we are ready to run the script.
This is a full script.
<?php
use Grpc\ChannelCredentials;
use Mt4grpc\ConnectRequest;
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/generated/GPBMetadata/Mt4.php';
$files = glob(__DIR__ . '/generated/Mt4grpc/*.php');
foreach ($files as $file) {
require($file);
}
function toArray($rf)
{
$ret = array();
foreach ($rf as $elem) {
array_push($ret, $elem);
}
return $ret;
}
// Connecting to the Mt4 server
$serviceConnectionClient = new Mt4grpc\ConnectionClient('mt4grpc.mtapi.io:443', ['credentials' => Grpc\ChannelCredentials::createSsl()]);
$connectionRequest = new Mt4grpc\ConnectRequest();
$connectionRequest -> setHost("mt4-demo.roboforex.com");
$connectionRequest -> setPassword("ehj4bod");
$connectionRequest -> setPort("443");
$connectionRequest -> setUser(500476959);
list($connectionResponse, $status) = $serviceConnectionClient->Connect($connectionRequest)->wait();
/** @var Mt4grpc\Error */
$connectionError = $connectionResponse->getError();
$connectionResult = $connectionResponse->getResult();
if (is_null($connectionError)) {
print($connectionResult);
print("\n");
} else {
print($connectionError . "\n");
print('Press enter to exit...');
fgets(STDIN);
exit;
}
// Getting account balance
$serviceMt4 = new Mt4grpc\MT4Client('mt4grpc.mtapi.io:443', ['credentials' => Grpc\ChannelCredentials::createSsl()]);
$req = new Mt4grpc\AccountSummaryRequest();
$req->setId($connectionResult);
list($response, $status) = $serviceMt4->AccountSummary($req)->wait();
/** @var Mt4grpc\AccountSummaryReply */
$reply = $response;
/** @var Mt4grpc\Error */
$error = $reply->getError();
/** @var Google\Protobuf\Internal\RepeatedField */
$result = $reply->getResult();
if (is_null($error)) {
/** @var Mt4grpc\AccountSummary */
print($result->getBalance() . ",");
print("\n");
} else {
print($error . "\n");
print('Press enter to exit...');
fgets(STDIN);
exit;
}
// Subscribing on the quote
$serviceSubscriptionClient = new Mt4grpc\SubscriptionsClient('mt4grpc.mtapi.io:443', ['credentials' => Grpc\ChannelCredentials::createSsl()]);
$subscribeReq = new Mt4grpc\SubscribeRequest();
$subscribeReq -> setId($connectionResult);
$subscribeReq -> setInterval(0);
$subscribeReq -> setSymbol("EURUSD");
list($subscriptionResponse, $status) = $serviceSubscriptionClient->Subscribe($subscribeReq)->wait();
/** @var Mt4grpc\Error */
$subscriptionError = $subscriptionResponse->getError();
$subscriptionResult = $subscriptionResponse->getResult();
/** @var Mt4grpc\SubscribeReply */
if (is_null($subscriptionError)) {
print($subscriptionResult . ",");
print("\n");
} else {
print($error . "\n");
print('Press enter to exit...');
fgets(STDIN);
exit;
}
// Receiving quotes
$streamsClient = new Mt4grpc\StreamsClient('mt4grpc.mtapi.io:443', ['credentials' => Grpc\ChannelCredentials::createSsl()]);
$OnQuoteReq = new Mt4grpc\OnQuoteRequest();
$OnQuoteReq -> setId($connectionResult);
$call = $streamsClient->OnQuote($OnQuoteReq);
$quotes = $call->responses();
foreach ($quotes as $quote) {
// process each quote
$onQuoteError = $quote->getError();
if (is_null($onQuoteError)) {
$quoteData = $quote->getResult();
if (is_null($quoteData)) {
print("Quote is empty");
}
else {
$symbol = $quoteData->getSymbol();
$bid = $quoteData->getBid();
$ask = $quoteData->getAsk();
$time = $quoteData->getTime();
$high = $quoteData->getHigh();
$low = $quoteData->getLow();
print("Symbol:" . $symbol . ",");
print("Bid:" . $bid . ",");
print("Ask:" . $ask . ",");
// Represents seconds of UTC time since Unix epoch 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.
$secondsSince1970 = $time->getSeconds();
$adate="Thu Jan 1 07:59:59 1970";
$dateinsec=strtotime($adate);
$newdate=$dateinsec+$secondsSince1970;
print("Time:" . date('D M H:i:s Y',$newdate) . ",");
print("High:" . $high . ",");
print("Low:" . $low . ",");
}
print("\n");
} else {
print($onQuoteError->getMessage() . "\n");
print('Press enter to exit...');
fgets(STDIN);
exit;
}
}
print('Press enter to exit...');
fgets(STDIN);