The official PHP client library for the Polygon REST APIs.
Polygon PHP Client - RESTful APIs
Welcome to the official PHP client library for the Polygon REST and WebSocket API. To get started, please see the Getting Started section in our documentation, view the API documentation directory for code snippets, or see the Model documentation to learn more about the data structures.
This version is an experimental release and is under active development. We are working to add more features and improve its stability and performance. Please be aware that this version might undergo significant changes, and some functionality may be incomplete or subject to modification.
Roadmap Features
- Pagination over nexturl and combine result sets: We plan to introduce enhanced pagination capabilities that will allow users to easily navigate through API responses using the nexturl provided and seamlessly combine result sets for comprehensive data analysis.
- Adding Tracing: To aid in debugging and enhance transparency, we aim to implement tracing features that will allow users to log detailed information about their API requests. This includes the requested URL, parameters, and full request and response headers.
- WebSocket Support: In our effort to provide real-time data access, we will be integrating WebSocket support into the client library. This will enable users to establish persistent connections for live data feeds with minimal latency.
Installation & Usage
Prerequisites
Before installing the Polygon PHP client, ensure your environment has PHP 7.4 or higher, compatible with PHP 8.0.
Composer
To install the bindings via Composer, add the following to composer.json:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/polygon-io/client-php.git"
}
],
"require": {
"GITUSERID/GITREPOID": "*@dev"
}
}
Then run composer install
Manual Installation
Download the files and include autoload.php:
<?php
require_once('/path/to/PolygonClient/vendor/autoload.php');
Getting Started
To get started, please follow the installation procedure and then explore the API documentation directory for code snippets, or see the Model documentation to learn more about the data structures.
The free tier of our API comes with usage limitations, which might lead to rate limit errors if these limits are exceeded. For uninterrupted access and to support larger data requirements, we recommend reviewing our subscription plans, which are tailored for a wide range of needs from development to high-demand enterprise applications. Visit our pricing page for detailed information on limits and features to ensure a seamless experience, especially for real-time data processing.
REST API Client
Import and configure the OpenAPI Client.
<?php
require_once(DIR . '/vendor/autoload.php');
// Load API key from environment variable $apiKey = getenv('POLYGONAPIKEY');
// Check if the API key is not empty if (!$apiKey) { die('API key is not set in environment variables.'); }
// Configure API key authorization: apiKey $config = OpenAPI\Client\Configuration::getDefaultConfiguration()->setApiKey('apiKey', $apiKey);
Using the Client
Request data using client methods.
// https://polygon.io/docs/stocks/getv2lasttrade_stocksticker $apiInstance = new OpenAPI\Client\Api\StockslasttradeApi( new GuzzleHttp\Client(), $config );
$stocks_ticker = "TSLA";
try { $result = $apiInstance->lastTrade($stocks_ticker); print_r($result); } catch (Exception $e) { echo 'Exception when calling StockslasttradeApi->lastTrade: ', $e->getMessage(), PHP_EOL; }
API Endpoints
All URIs are relative to api.polygon.io:
Class | Method | HTTP request | Description ------------ | ------------- | ------------- | ------------- CrpytoaggregatesApi | cryptoEMA | GET /v1/indicators/ema/{cryptoTicker} | Exponential Moving Average (EMA) CrpytoaggregatesApi | cryptoRSI | GET /v1/indicators/rsi/{cryptoTicker} | Relative Strength Index (RSI) CrpytoaggregatesApi | cryptoSMA | GET /v1/indicators/sma/{cryptoTicker} | Simple Moving Average (SMA) CryptoaggregatesApi | cryptoMACD | GET /v1/indicators/macd/{cryptoTicker} | Moving Average Convergence/Divergence (MACD) CryptoaggregatesApi | v2AggsGroupedLocaleGlobalMarketCryptoDateGet | GET /v2/aggs/grouped/locale/global/market/crypto/{date} | Grouped Daily (Bars) CryptoaggregatesApi | v2AggsTickerCryptoTickerPrevGet | GET /v2/aggs/ticker/{cryptoTicker}/prev | Previous Close CryptoaggregatesApi | v2AggsTickerCryptoTickerRangeMultiplierTimespanFromToGet | GET /v2/aggs/ticker/{cryptoTicker}/range/{multiplier}/{timespan}/{from}/{to} | Aggregates (Bars) CryptolasttradeApi | lastTradeCrypto | GET /v1/last/crypto/{from}/{to} | Last Trade for a Crypto Pair CryptoopenCloseApi | v1OpenCloseCryptoFromToDateGet | GET /v1/open-close/crypto/{from}/{to}/{date} | Daily Open/Close CryptosnapshotApi | v2SnapshotLocaleGlobalMarketsCryptoDirectionGet | GET /v2/snapshot/locale/global/markets/crypto/{direction} | Gainers/Losers CryptosnapshotApi | v2SnapshotLocaleGlobalMarketsCryptoTickersGet | GET /v2/snapshot/locale/global/markets/crypto/tickers | All Tickers CryptosnapshotApi | v2SnapshotLocaleGlobalMarketsCryptoTickersTickerBookGet | GET /v2/snapshot/locale/global/markets/crypto/tickers/{ticker}/book | Ticker Full Book (L2) CryptosnapshotApi | v2SnapshotLocaleGlobalMarketsCryptoTickersTickerGet | GET /v2/snapshot/locale/global/markets/crypto/tickers/{ticker} | Ticker CryptotradesApi | tradesCrypto | GET /v3/trades/{cryptoTicker} | Trades CryptotradesApi | v1HistoricCryptoFromToDateGet | GET /v1/historic/crypto/{from}/{to}/{date} | Historic Crypto Trades DefaultApi | snapshotSummary | GET /v1/summaries | Summaries DefaultApi | snapshots | GET /v3/snapshot | Universal Snapshot FxaggregatesApi | forexEMA | GET /v1/indicators/ema/{fxTicker} | Exponential Moving Average (EMA) FxaggregatesApi | forexMACD | GET /v1/indicators/macd/{fxTicker} | Moving Average Convergence/Divergence (MACD) FxaggregatesApi | forexRSI | GET /v1/indicators/rsi/{fxTicker} | Relative Strength Index (RSI) FxaggregatesApi | forexSMA | GET /v1/indicators/sma/{fxTicker} | Simple Moving Average (SMA) FxaggregatesApi | v2AggsGroupedLocaleGlobalMarketFxDateGet | GET /v2/aggs/grouped/locale/global/market/fx/{date} | Grouped Daily (Bars) FxaggregatesApi | v2AggsTickerForexTickerPrevGet | GET /v2/aggs/ticker/{forexTicker}/prev | Previous Close FxaggregatesApi | v2AggsTickerForexTickerRangeMultiplierTimespanFromToGet | GET /v2/aggs/ticker/{forexTicker}/range/{multiplier}/{timespan}/{from}/{to} | Aggregates (Bars) FxconversionApi | realTimeCurrencyConversion | GET /v1/conversion/{from}/{to} | Real-time Currency Conversion FxlastquoteApi | lastQuoteCurrencies | GET /v1/lastquote/currencies/{from}/{to} | Last Quote for a Currency Pair FxquotesApi | quotesFx | GET /v3/quotes/{fxTicker} | Quotes (BBO) FxsnapshotApi | v2SnapshotLocaleGlobalMarketsForexDirectionGet | GET /v2/snapshot/locale/global/markets/forex/{direction} | Gainers/Losers FxsnapshotApi | v2SnapshotLocaleGlobalMarketsForexTickersGet | GET /v2/snapshot/locale/global/markets/forex/tickers | All Tickers FxsnapshotApi | v2SnapshotLocaleGlobalMarketsForexTickersTickerGet | GET /v2/snapshot/locale/global/markets/forex/tickers/{ticker} | Ticker FxtradesApi | v1HistoricForexFromToDateGet | GET /v1/historic/forex/{from}/{to}/{date} | Historic Forex Ticks IndicesaggregatesApi | indicesEMA | GET /v1/indicators/ema/{indicesTicker} | Exponential Moving Average (EMA) IndicesaggregatesApi | indicesMACD | GET /v1/indicators/macd/{indicesTicker} | Moving Average Convergence/Divergence (MACD) IndicesaggregatesApi | indicesRSI | GET /v1/indicators/rsi/{indicesTicker} | Relative Strength Index (RSI) IndicesaggregatesApi | indicesSMA | GET /v1/indicators/sma/{indicesTicker} | Simple Moving Average (SMA) IndicesaggregatesApi | v2AggsTickerIndicesTickerPrevGet | GET /v2/aggs/ticker/{indicesTicker}/prev | Previous Close IndicesaggregatesApi | v2AggsTickerIndicesTickerRangeMultiplierTimespanFromToGet | GET /v2/aggs/ticker/{indicesTicker}/range/{multiplier}/{timespan}/{from}/{to} | Aggregates (Bars) IndicessnapshotApi | indicesSnapshot | GET /v3/snapshot/indices | Indices Snapshot InternalApi | listTickerTaxonomyClassifications | GET /vX/reference/tickers/taxonomies | Ticker Taxonomies OptionsaggregatesApi | optionsEMA | GET /v1/indicators/ema/{optionsTicker} | Exponential Moving Average (EMA) OptionsaggregatesApi | optionsMACD | GET /v1/indicators/macd/{optionsTicker} | Moving Average Convergence/Divergence (MACD) OptionsaggregatesApi | optionsRSI | GET /v1/indicators/rsi/{optionsTicker} | Relative Strength Index (RSI) OptionsaggregatesApi | optionsSMA | GET /v1/indicators/sma/{optionsTicker} | Simple Moving Average (SMA) OptionsaggregatesApi | v2AggsTickerOptionsTickerPrevGet | GET /v2/aggs/ticker/{optionsTicker}/prev | Previous Close OptionsaggregatesApi | v2AggsTickerOptionsTickerRangeMultiplierTimespanFromToGet | GET /v2/aggs/ticker/{optionsTicker}/range/{multiplier}/{timespan}/{from}/{to} | Aggregates (Bars) OptionslasttradeApi | lastTradeOptions | GET /v2/last/trade/{optionsTicker} | Last Trade OptionsopenCloseApi | v1OpenCloseOptionsTickerDateGet | GET /v1/open-close/{optionsTicker}/{date} | Daily Open/Close OptionsquotesApi | quotesOptions | GET /v3/quotes/{optionsTicker} | Quotes OptionssnapshotApi | optionContract | GET /v3/snapshot/options/{underlyingAsset}/{optionContract} | Option Contract OptionssnapshotApi | optionsChain | GET /v3/snapshot/options/{underlyingAsset} | Options Chain OptionstradesApi | tradesOptions | GET /v3/trades/{optionsTicker} | Trades PublicApi | listTickerTaxonomyClassifications | GET /vX/reference/tickers/taxonomies | Ticker Taxonomies ReferenceconditionsApi | listConditions | GET /v3/reference/conditions | Conditions ReferencedividendsApi | listDividends | GET /v3/reference/dividends | Dividends v3 ReferenceexchangesApi | listExchanges | GET /v3/reference/exchanges | Exchanges ReferencenewsApi | listNews | GET /v2/reference/news | Ticker News ReferenceoptionscontractApi | getOptionsContract | GET /v3/reference/options/contracts/{optionsticker} | Options Contract ReferenceoptionscontractslistApi | listOptionsContracts | GET /v3/reference/options/contracts | Options Contracts ReferencesecfilingApi | getFiling | GET /v1/reference/sec/filings/{filingid} | SEC Filing ReferencesecfilingfileApi | getFilingFile | GET /v1/reference/sec/filings/{filingid}/files/{file_id} | SEC Filing File ReferencesecfilingfilesApi | listFilingFiles | GET /v1/reference/sec/filings/{filingid}/files | SEC Filing Files ReferencesecfilingsApi | listFilings | GET /v1/reference/sec/filings | SEC Filings ReferencestocksApi | listFinancials | GET /vX/reference/financials | Stock Financials vX ReferencestocksApi | listStockSplits | GET /v3/reference/splits | Stock Splits v3 ReferencestocksmarketApi | getMarketHolidays | GET /v1/marketstatus/upcoming | Market Holidays ReferencestocksmarketApi | getMarketStatus | GET /v1/marketstatus/now | Market Status ReferencetickersgetApi | getEvents | GET /vX/reference/tickers/{id}/events | Ticker Events ReferencetickersgetApi | getTicker | GET /v3/reference/tickers/{ticker} | Ticker Details v3 ReferencetickerslistApi | listTickers | GET /v3/reference/tickers | Tickers ReferencetickerstypesApi | listTickerTypes | GET /v3/reference/tickers/types | Ticker Types StocksaggregatesApi | eMA | GET /v1/indicators/ema/{stockTicker} | Exponential Moving Average (EMA) StocksaggregatesApi | mACD | GET /v1/indicators/macd/{stockTicker} | Moving Average Convergence/Divergence (MACD) StocksaggregatesApi | rSI | GET /v1/indicators/rsi/{stockTicker} | Relative Strength Index (RSI) StocksaggregatesApi | sMA | GET /v1/indicators/sma/{stockTicker} | Simple Moving Average (SMA) StocksaggregatesApi | v2AggsGroupedLocaleUsMarketStocksDateGet | GET /v2/aggs/grouped/locale/us/market/stocks/{date} | Grouped Daily (Bars) StocksaggregatesApi | v2AggsTickerStocksTickerPrevGet | GET /v2/aggs/ticker/{stocksTicker}/prev | Previous Close StocksaggregatesApi | v2AggsTickerStocksTickerRangeMultiplierTimespanFromToGet | GET /v2/aggs/ticker/{stocksTicker}/range/{multiplier}/{timespan}/{from}/{to} | Aggregates (Bars) StockslastquoteApi | lastQuote | GET /v2/last/nbbo/{stocksTicker} | Last Quote StockslasttradeApi | lastTrade | GET /v2/last/trade/{stocksTicker} | Last Trade StocksopenCloseApi | v1OpenCloseIndicesTickerDateGet | GET /v1/open-close/{indicesTicker}/{date} | Daily Open/Close StocksopenCloseApi | v1OpenCloseStocksTickerDateGet | GET /v1/open-close/{stocksTicker}/{date} | Daily Open/Close StocksquotesApi | quotes | GET /v3/quotes/{stockTicker} | Quotes (NBBO) StocksquotesApi | v2TicksStocksNbboTickerDateGet | GET /v2/ticks/stocks/nbbo/{ticker}/{date} | Quotes (NBBO) StockssnapshotApi | v2SnapshotLocaleUsMarketsStocksDirectionGet | GET /v2/snapshot/locale/us/markets/stocks/{direction} | Gainers/Losers StockssnapshotApi | v2SnapshotLocaleUsMarketsStocksTickersGet | GET /v2/snapshot/locale/us/markets/stocks/tickers | All Tickers StockssnapshotApi | v2SnapshotLocaleUsMarketsStocksTickersStocksTickerGet | GET /v2/snapshot/locale/us/markets/stocks/tickers/{stocksTicker} | Ticker StockstradesApi | trades | GET /v3/trades/{stockTicker} | Trades StockstradesApi | v2TicksStocksTradesTickerDateGet | GET /v2/ticks/stocks/trades/{ticker}/{date} | Trades
Contributing
If you found a bug or have an idea for a new feature, please first discuss it with us by submitting a new issue. We are also open to volunteers willing to submit PRs for any open issues but please discuss it with us beforehand.
Development
For developing new features or fixing bugs, you will need to manage dependencies using Composer.
composer install
To run tests, use:
vendor/bin/phpunit
This PHP client follows the same release cadence as our API, ensuring it remains compatible with new and deprecated features.