PHP code example of swar8080 / fast-stock-quotes

1. Go to this page and download the library: Download swar8080/fast-stock-quotes 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/ */

    

swar8080 / fast-stock-quotes example snippets




use FastStockQuotes\FastQuoteServiceBuilder;
use FastStockQuotes\StockSymbol;
use FastStockQuotes\markets\ExchangeCodes;
use FastStockQuotes\quoteAPIs\USQuote;

$quoteService = FastQuoteServiceBuilder::builder()
	->withAlphaVantageGlobalQuoteAPI("your API key")
	->withRedisCaching(new \Predis\Client(), $minCachingLengthSeconds=300)
	->build();

//For US stocks, just pass the symbol
$usSymbol = new StockSymbol("MSFT");

//For non-us stocks, an exchange code must be added to the stock symbol to identify the stock exchange it belongs to.
//If you're unsure of the exchange code, use the built-in ExchangeCodes constant or check Yahoo Finance
$canadianSymbol = new StockSymbol("SHOP", $exchangeCode=ExchangeCodes::CANADA); 
$australianSymbol = new StockSymbol("WBC.AX"); //equivalent to: new StockSymbol("WBC", ExchangeCodes::AUSTRALIA)

//request the quotes from the appropriate APIs
$quotes = $quoteService->quotes(array($usSymbol, $canadianSymbol, $australianSymbol));

//this is equivalent
$quotes = $quoteService->quotes(StockSymbol::Symbols("MSFT", "SHOP.TO", "WBC.AX"));

foreach ($quotes as $symbol => $quote){
	echo "--" . $quote->symbol() . "--" . PHP_EOL;
	echo $quote->price() . PHP_EOL;
	echo $quote->open() . PHP_EOL;
	echo $quote->previousDayClose() . PHP_EOL;
	echo $quote->high() . PHP_EOL;
	echo $quote->low() . PHP_EOL;
	echo $quote->volume() . PHP_EOL;
	echo $quote->lastUpdated() . PHP_EOL;

	if ($quote instanceof USQuote){
		echo $quote->marketCap . PHP_EOL;
		echo $quote->week52High . PHP_EOL;
		echo $quote->peRatio . PHP_EOL;
		//full list of fields available for US quotes: https://iextrading.com/developer/docs/#quote
	}
	echo PHP_EOL;
}