Specifications

LISTING 17.1 lookup.phpScript Retrieves a Stock Quote from the NASDAQ for the
Stock with the Ticker Symbol Listed in $symbol
<html>
<head>
<title>Stock Quote from NASDAQ</title>
</head>
<body>
<?
// choose stock to look at
$symbol=”AMZN”;
echo “<h1>Stock Quote for $symbol</h1>”;
// connect to URL and read information
$theurl = “http://quotes.nasdaq-amex.com/Quote.dll?”
.”page=multi&mode=Stock&symbol=”.$symbol;
if (!($fp = fopen($theurl, “r”)))
{
echo “Could not open URL”;
exit;
}
$contents = fread($fp, 1000000);
fclose($fp);
// find the part of the page we want and output it
$pattern = “(\\\$[0-9 ]+\\.[0-9]+)”;
if (eregi($pattern, $contents, $quote))
{
echo “$symbol was last sold at: “;
echo $quote[1];
} else
{
echo “No quote available”;
};
// acknowledge source
echo “<br>”
.”This information retrieved from <br>”
.”<a href=\”$theurl\”>$theurl</a><br>”
.”on “.(date(“l jS F Y g:i a T”));
?>
</body>
</html>
The output from one sample run of Listing 17.1 is shown in Figure 17.1.
Advanced PHP Techniques
P
ART IV
372
22 7842 CH17 3/6/01 3:39 PM Page 372