PHP code example of polygon-io / api

1. Go to this page and download the library: Download polygon-io/api 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/ */

    

polygon-io / api example snippets






ad API key from environment variable
$apiKey = getenv('POLYGON_API_KEY');

// 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);


// https://polygon.io/docs/stocks/get_v2_last_trade__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;
}