PHP code example of scheb / yahoo-finance-api

1. Go to this page and download the library: Download scheb/yahoo-finance-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/ */

    

scheb / yahoo-finance-api example snippets


use Scheb\YahooFinanceApi\ApiClient;
use Scheb\YahooFinanceApi\ApiClientFactory;
use GuzzleHttp\Client;

// Create a new client from the factory
$client = ApiClientFactory::createApiClient();

// Or configure with options
$client = ApiClientFactory::createApiClient(
    clientOptions: [/* ... */], // Guzzle client options
    retries: 3,
    retryDelay: 1000, // milliseconds
);

// Returns an array of Scheb\YahooFinanceApi\Results\SearchResult
$searchResult = $client->search("Apple");

// Returns an array of Scheb\YahooFinanceApi\Results\HistoricalData
$historicalData = $client->getHistoricalQuoteData(
    "AAPL",
    ApiClient::INTERVAL_1_DAY,
    new \DateTime("-14 days"),
    new \DateTime("today")
);

// Retrieve dividends history, returns an array of Scheb\YahooFinanceApi\Results\DividendData
$dividendData = $client->getHistoricalDividendData(
    "AAPL",
    new \DateTime("-5 years"),
    new \DateTime("today")
);

// Retrieve stock split history, returns an array of Scheb\YahooFinanceApi\Results\SplitData
$splitData = $client->getHistoricalSplitData(
    "AAPL",
    new \DateTime("-5 years"),
    new \DateTime("today")
);

// Returns Scheb\YahooFinanceApi\Results\Quote
$exchangeRate = $client->getExchangeRate("USD", "EUR");

// Returns an array of Scheb\YahooFinanceApi\Results\Quote
$exchangeRates = $client->getExchangeRates([
    ["USD", "EUR"],
    ["EUR", "USD"],
]);

// Returns Scheb\YahooFinanceApi\Results\Quote
$quote = $client->getQuote("AAPL");

// Returns an array of Scheb\YahooFinanceApi\Results\Quote
$quotes = $client->getQuotes(["AAPL", "GOOG"]);

// Returns an array of Scheb\YahooFinanceApi\Results\OptionChain
$optionChain = $client->getOptionChain("AAPL");
$optionChain = $client->getOptionChain("AAPL", new \DateTime("2021-01-01"));

$guzzleClientOptions = ['headers' => ['User-Agent' => 'MyApp/1.0']];
$client = ApiClientFactory::createApiClient($guzzleClientOptions);

$client = ApiClientFactory::createApiClient();

$client = ApiClientFactory::createApiClient(
    retries: 3,        // Number of retry attempts (default: 0)
    retryDelay: 1000,  // Delay between retries in milliseconds (default: 0)
);

use Scheb\YahooFinanceApi\ApiClientFactory;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

// File-based cache from symfony/cache
$cache = new FilesystemAdapter();

// Create API client with caching
$client = ApiClientFactory::createApiClient(
    cache: $cache,              // PSR-6 cache implementation
    cacheTtl: 3600,             // Cache TTL in seconds (optional, default: 3600)
    cacheKey: 'my_cache_key'    // Custom cache key (optional)
);