PHP Classes

File: readme.md

Recommend this page to a friend!
  Packages of Besim Huskic   InvestorySpot Stock Ticker   readme.md   Download  
File: readme.md
Role: Documentation
Content type: text/markdown
Description: readme file
Class: InvestorySpot Stock Ticker
Fetch stock quotes and display them in a ticker
Author: By
Last change:
Date: 4 months ago
Size: 3,208 bytes
 

Contents

Class file image Download

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',
]