PHP Classes

How to Use a PHP Stock Price Retriever Class to Display in a Ticker Using the Package InvestorySpot Stock Ticker: Fetch stock quotes and display them in a ticker

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-07-07 (4 months ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
stock-ticker 1.0.0MIT/X Consortium ...7.4HTML, PHP 5, Web services, Finances
Description 

Author

This package can fetch stock quotes and display them in a ticker.

It provides a class that can send HTTP requests to different providers to retrieve stock quotes.

The class can also generate HTML to display a ticker with the retrieved stock quotes in a Web page.

Currently it supports the following providers:

- Yahoo Finance

- Alpha Vantage

- Finnhub

Picture of Besim Huskic
  Performance   Level  
Name: Besim Huskic <contact>
Classes: 3 packages by
Country: Bosnia and Herzegovina Bosnia and Herzegovina
Age: 48
All time rank: Not yet ranked
Week rank: Not yet ranked
Innovation award
Innovation award
Nominee: 1x

Instructions

Please read this document to learn how to install, configure, and display a stock ticker on a web page.

Example

<?php
require_once __DIR__ . '/../vendor/autoload.php';

use InvestorySpot\StockTicker\StockTicker;

$ticker = new StockTicker(__DIR__ . '/cache');
$ticker->set_refresh(300);
$quotes = $ticker->get_quotes('AAPL,GOOGL,MSFT,AMZN,NVDA,TSLA,META');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stock Ticker Demo</title>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; background: #111; color: #eee; }
  .stock-ticker-wrap { overflow: hidden; white-space: nowrap; background: #1a1a2e; padding: 12px 0; border-bottom: 1px solid #333; }
  .stock-ticker-track { display: inline-block; animation: ticker-scroll 40s linear infinite; }
  @keyframes ticker-scroll { 0% { transform: translateX(0); } 100% { transform: translateX(-50%); } }
  .stock-ticker-item { display: inline-block; padding: 0 24px; font-size: 15px; }
  .stock-ticker-symbol { font-weight: 700; margin-right: 6px; color: #fff; }
  .stock-ticker-price { font-weight: 600; margin-right: 4px; }
  .stock-ticker-change { font-size: 13px; }
  .up { color: #00a32a; }
  .down { color: #d63638; }
  main { max-width: 800px; margin: 40px auto; padding: 0 20px; }
  h1 { font-size: 24px; }
  table.stock-ticker-table { width: 100%; border-collapse: collapse; margin-top: 20px; }
  table.stock-ticker-table th, table.stock-ticker-table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid #333; }
  table.stock-ticker-table th { background: #1a1a2e; font-weight: 600; }
</style>
</head>
<body>

<?php echo $ticker->render_ticker($quotes); ?>

<main>
  <h1>Stock Quotes</h1>
  <?php echo $ticker->render_table($quotes); ?>

  <p style="margin-top:24px;color:#888;font-size:13px;">
    Data refreshes every <?php echo $ticker->get_refresh(); ?> seconds.
    Provider: <?php echo $quotes[array_key_first($quotes)]['provider']; ?>.
  </p>
</main>

</body>
</html>


Details

StockTicker

Standalone PHP class for fetching stock quotes with multi-provider fallback (Yahoo Finance ? Alpha Vantage ? Finnhub).

Requirements

  • PHP 5.6+
  • cURL extension
  • `json` extension

Install via Composer

composer require investoryspot/stock-ticker

Or add to composer.json:

{
    "require": {
        "investoryspot/stock-ticker": "^1.0"
    }
}

The package uses PSR-4 autoloading: InvestorySpot\StockTicker\StockTicker.

Quick Start

require_once 'vendor/autoload.php';

use InvestorySpot\StockTicker\StockTicker;

$ticker = new StockTicker(__DIR__ . '/cache');
$quotes = $ticker->get_quotes('AAPL,GOOGL,MSFT');

Table output (no CSS needed ? colors are inline)

echo $ticker->render_table($quotes);

Ticker output (requires CSS ? see below)

echo $ticker->render_ticker($quotes);

Configuration

API keys (optional fallbacks)

Define these constants before instantiation. Yahoo Finance works without any key.

define('ALPHAVANTAGE_API_KEY', 'your-key');
define('FINNHUB_API_KEY', 'your-key');

Cache TTL & colors

$ticker->set_refresh(120);                  // seconds (min 60)
$ticker->set_colors('#00a32a', '#d63638');  // up / down

Minimal CSS for ticker scroll

.stock-ticker-wrap { overflow: hidden; white-space: nowrap; }
.stock-ticker-track { display: inline-block; animation: ticker-scroll 30s linear infinite; }
@keyframes ticker-scroll {
  0%   { transform: translateX(0); }
  100% { transform: translateX(-50%); }
}
.stock-ticker-item { display: inline-block; padding: 0 20px; }
.up   { color: #00a32a; }
.down { color: #d63638; }

Optional JS auto-refresh

Add this snippet on the page to re-fetch quotes every N seconds (matching your cache TTL).

<script>
(function(){
  var wrap = document.querySelector('.stock-ticker-wrap') ||
             document.querySelector('.stock-ticker-table-wrap');
  if (!wrap) return;
  var refresh = parseInt(wrap.dataset.refresh, 10);
  if (!refresh) return;
  setInterval(function(){
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'your-endpoint.php');      // ? your server endpoint
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onload = function(){
      if (xhr.status === 200) {
        var el = document.querySelector('.stock-ticker-wrap') ||
                 document.querySelector('.stock-ticker-table-wrap');
        if (el) el.outerHTML = xhr.responseText;
      }
    };
    xhr.send('symbols=AAPL,GOOGL,MSFT&display=ticker');
  }, refresh * 1000);
})();
</script>

Provider status

$ticker->get_provider_status();
// ['yahoo' => true, 'alphavantage' => false, 'finnhub' => false]

Data format

Each quote returned by get_quotes():

[
  'symbol'         => 'AAPL',
  'price'          => 198.50,
  'change'         => 1.25,
  'change_percent' => 0.63,
  'open'           => 197.00,
  'high'           => 199.10,
  'low'            => 196.80,
  'volume'         => 48200000,
  'currency'       => 'USD',
  'exchange'       => 'NMS',
  'marketState'    => 'REGULAR',
  'provider'       => 'yahoo',
]

  Files folder image Files (7)  
File Role Description
Files folder imageexamples (3 files, 1 directory)
Files folder imagesrc (1 file)
Accessible without login Plain text file licence Lic. licence
Accessible without login Plain text file readme.md Doc. readme file

  Files folder image Files (7)  /  examples  
File Role Description
Files folder imagecache (1 file)
  Accessible without login Plain text file basic.php Example php file
  Accessible without login Plain text file server-endpoint.php Example php file
  Accessible without login Plain text file ticker-page.php Example php file

  Files folder image Files (7)  /  examples  /  cache  
File Role Description
  Accessible without login Plain text file a.gitkeep Example git file

  Files folder image Files (7)  /  src  
File Role Description
  Plain text file StockTicker.php Class source file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 0%
Total:0
This week:0