Every web site developer is looking for those little hooks that will keep people coming back to their pages - one of those hooks could be using PHP to show current stock market information from Yahoo! Finance. Fortunately this is not difficult to set up - all that the web site developer needs to do is:
- create a library to be used in one or more web pages
- write a function that will access and process the data from Yahoo! Finance
- load the library and use the function wherever they need it
However, before setting up that PHP library it's worth looking at the data that's going to be coming from the Yahoo! Finance web site.
Yahoo! Finance Data
Current stock quotes can be obtained quite easily from Yahoo! Finance:
- the stock quotes are obtained via the url - http://quote.yahoo.com/d/quotes.csv
- the url needs some additional pieces information, and these are:
- s - a list of companies or markets for which stock quotes are required. Yahoo! Finance symbols are entered rather than the full name, for example:
- ^IXIC = NASDAQ Composite Average
- ^NIN = NYSE International 100
- ^FTSE = FTSE 100
- RHT = Red Hat Inc.
- f - a list of fields to be output, such as:
- n - the company name
- l1 - the latest stock quote
- c1 - the change in value of the stock price
- e=.csv - the output type will be in csv (comma separated variable format)
- s - a list of companies or markets for which stock quotes are required. Yahoo! Finance symbols are entered rather than the full name, for example:
So, for example to examine the current state of some of the stock markets the full web url would be:
Obviously the next step is to write a PHP function that will read the information that this url supplies as a data stream.
A PHP Function to Process Yahoo! Finance Data
A PHP library is created by saving the function into a text file - in this example the library is going to be called yahoo_finance.php and this will contain a single function:
The function will, by default, return the details of some of the stock markets:
The function will then use either an input list or the default list to create the correct url:
and then read in the data stream from the Yahoo! Finance web site:
The data will be in the format:
and so the next stage is to load the data into an array (using the new line as a delimiter):
finally the results can be output (taking care to remove the additional quotation marks):
Using the PHP Function to Display Yahoo! Finance Data
Once the function and library have been saved then they can be used in a PHP web page (for instance index.php) to display the default stock market data:
or used in a PHP file that can process the input from a textarea box:
If this code is stored in a file (for example yahoo_finance_lookup.php) then it can be called from a form:
Conclusion
Yahoo! Finance is a very useful resource for both analysts and programmers alike, but if it also make a web site more attractive then it must be worth considering - especially when it's so easy to incorporate into PHP.
Additional Reading
Using Yahoo! Financial Stock Quotes