RE: [PHP] Retrieving data from a URL in PHP
| From: | Scott Hurring | Date: | Wed, 05 Jun 2002 20:33:09 +0000 |
| Subject: | RE: [PHP] Retrieving data from a URL in PHP | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-100789@lists.php.net to get a copy of this message | ||
To illustrate what's wrong with the code from the book,
the following code works... notice what's different.
But really, you should've just loaded the URL and noticed
there's no "$[0-9].[0-9]" and instead it was bold.
problem solved.
$symbol="ibm";
echo "<h1>Stock Quote for $symbol</h1>\n";
$theurl="http://finance.yahoo.com/q?s={$symbol}&d=v1";
if (!($fp=fopen($theurl, "r")))
{
echo "Could not open the URL";
exit;
}
$contents= fread($fp, 1000000);
fclose($fp);
if (preg_match("/(<b>[0-9]+\.[0-9]+<\/b>)/",$contents, $quote))
{
echo "$symbol was sold at: ";
echo $quote[1];
}
else
{
echo "No quote available.";
}
echo "<br>"."This information retrieved from<br>"."<a
href=\"$theurl\">$theurl</a><br>"."on ".(date("l jS F Y
g:i a T"));
---
Scott Hurring
Systems Programmer
EAC Corporation
ScottH@eac.com
Voice: 201-462-2149
Fax: 201-288-1515
> -----Original Message-----
> From: Anthony Ritter [mailto:tony@gonefishingguideservice.com]
> Sent: Wednesday, June 05, 2002 4:53 PM
> Subject: [PHP] Retrieving data from a URL in PHP
>
> The code that follows is from Welling and Thomson's book on
> PHP and mysql
> (page 372)
>
> I've tried it on Apache/ MS Windows 98 / PHP and I get the
> following line:
>
> "No quote available."
>
> My questions...
>
> 1. Why can't I retrieve the present stock quote?
>
> and
>
> 2. What does the variable
>
> $quote
>
> represent in the script?
>
> I cannot find what the variable -or array - $quote has been
> assigned to.
> Please see:
>
> echo $quote[1];
>
> below
>
> Thanking all in advance.
> Tony Ritter
> ......................................................................
>
>
> <html>
> <head>
> <title>Stock Quote from NYSE</title>
> </head>
> <body>
> <?
> $symbol="IBM";
> echo "<h1>Stock Quote for $symbol</h1>";
> $theurl="http://finance.yahoo.com/q?s=ibm&d=v1";
> if (!($fp=fopen($theurl, "r")))
> {
> echo "Could not open the URL";
> exit;
> }
> $contents= fread($fp, 1000000);
> fclose($fp);
> $pattern="(\\\$[0-9]+\\.[0-9]+)";
> if (eregi($pattern,$contents,$quote))
> {
> echo "$symbol was sold at: ";
> echo $quote[1];
> }
> else
> {
> echo "No quote available.";
> }
> 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>
>
>
>
>
>
>
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>