mygrainexchange/mgx-php
Composer 安装命令:
composer require mygrainexchange/mgx-php
包简介
Official PHP client for the MGX Enterprise API. Browse anonymized inventory, place bids, read your team's trades and bids, manage cash bids, subscribe to webhooks, and read market prices. Authenticate with OAuth2 (Login with MGX). Seller identity is never exposed until a trade is paid on both sides.
README 文档
README
Official PHP client for the MGX Enterprise API.
composer require mygrainexchange/mgx-php
Requires PHP 8.1+.
Quickstart
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; }
You can also construct with an options array:
$mgx = MgxClient::create([ 'clientId' => getenv('MGX_CLIENT_ID'), 'clientSecret' => getenv('MGX_CLIENT_SECRET'), 'scopes' => ['inventory.read', 'market.read'], ]);
Authentication
- Client credentials (read-only data) — pass
clientId+clientSecret+scopes; the SDK acquires, caches, and refreshes the token for you. - Login with MGX (user-context: bids, trades, teams, cash bids, webhooks) — complete the authorization-code + PKCE flow, then pass the resulting
accessToken(and optionallyrefreshToken) toMgxClient.
Features
- Resource namespaces —
inventory(),market(),bids(),trades(),teams(),cashBids(),webhooks(). - Auto-pagination — every
list()returns aPaginatoryou canforeachover; it followsnextuntil the result set is exhausted (->toArray()collects eagerly). - Idempotency —
inventory()->placeBid()andcashBids()->create()send anIdempotency-Keyautomatically (override with the$idempotencyKeyargument). - Typed errors — non-2xx responses throw
MgxApiErrorwithstatus(),code(),getMessage(), andfieldErrors().
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()}"); }
Generated code
The lib/ client is generated from the OpenAPI spec; the ergonomic layer lives in
overlay/ (namespace MyGrainExchange\Mgx\Overlay). Do not hand-edit lib/ —
change the API spec and regenerate. See the
mgx-sdks repo.
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-19