How can PHP be used to create custom events and alerts based on real-time stock market data?

To create custom events and alerts based on real-time stock market data using PHP, you can utilize APIs from financial data providers like Alpha Vantage or Yahoo Finance to fetch the latest stock prices. You can then set up a script to continuously monitor the data and trigger custom events or alerts based on predefined conditions such as price thresholds or percentage changes.

// Example code snippet to fetch real-time stock data using Alpha Vantage API

$apiKey = 'YOUR_API_KEY';
$symbol = 'AAPL'; // Stock symbol for Apple Inc.

$url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=$symbol&apikey=$apiKey";

$data = json_decode(file_get_contents($url), true);

if(isset($data['Global Quote'])) {
    $latestPrice = $data['Global Quote']['05. price'];
    
    // Custom condition to trigger an alert
    if($latestPrice > 150) {
        // Send an email notification or log the event
        echo "Alert: Stock price for $symbol is above $150!";
    }
}