PHP code example of mygrainexchange / mgx-php

1. Go to this page and download the library: Download mygrainexchange/mgx-php 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/ */

    

mygrainexchange / mgx-php example snippets


use MyGrainExchange\Mgx\Overlay\MgxClient;
use MyGrainExchange\Mgx\Overlay\MgxApiError;

$mgx = new MgxClient(
    clientId: getenv('MGX_CLIENT_ID'),
    clientSecret: getenv('MGX_CLIENT_SECRET'),
    scopes: ['inventory.read', 'market.read'],
    // baseUrl: 'https://dashboard.mgx.test/v1', // for local development
);

// Auto-paginates the { items, limit, offset, next } envelope.
foreach ($mgx->inventory()->list(['commodity' => 'wheat', 'minQuantity' => 50]) as $lot) {
    echo $lot->getId(), ' ', $lot->getQuantityMt(), ' ', $lot->getAskingPrice()?->getAmount(), PHP_EOL;
}

$mgx = MgxClient::create([
    'clientId' => getenv('MGX_CLIENT_ID'),
    'clientSecret' => getenv('MGX_CLIENT_SECRET'),
    'scopes' => ['inventory.read', 'market.read'],
]);

try {
    $bid = $mgx->inventory()->placeBid('inv_3Kd9aZ', [
        'quantity_mt' => 50,
        'price' => ['amount' => 312.5],
        'delivery' => ['from' => '2026-08-01', 'to' => '2026-09-30'],
    ]);
} catch (MgxApiError $e) {
    error_log("{$e->status()} {$e->code()} {$e->getMessage()}");
}
bash
composer