Archive for the ‘PHP’ Category

PHP applications that involve accessing stock symbol prices, changes, and additional details on a repeated basis can be easily obtained from Yahoo. The below url will open an excel spreadsheet. Simply replace XXX with a comma delimited list of stock ticker symbols.

http://finance.yahoo.com/d/quotes.csv?s=XXX&f=sl1d1t1c1ohgv&e=.csv

The resulting spreadsheet displays a row for each ticker with the following informations from columns A to I: symbol, last price, date, time, change, open, high, low, and volume. To incorporate the above url into PHP, follow the below script that handles one ticker. We will use Google (GOOG) as an example. The script will output the result from the spreadsheet into an html table.

<?php
$fp = fopen (”http://finance.yahoo.com/d/quotes.csv?s=goog&f=sl1d1t1c1ohgv&e=.csv”,”r”);
$data = fgetcsv ($fp, 1000, “,”)
?>

<table>
<tr><td>description</td><td>latest figure</td><tr>
<tr><td>symbol</td><td><?php echo $data[0] ?></td></tr>
<tr><td>last price</td><td><?php echo $data[1] ?></td></tr>
<tr><td>date</td><td><?php echo $data[2] ?></td></tr>
<tr><td>time</td><td><?php echo $data[3] ?></td></tr>
<tr><td>change</td><td><?php echo $data[4] ?></td></tr>
<tr><td>open</td><td><?php echo $data[5] ?></td></tr>
<tr><td>high</td><td><?php echo $data[6] ?></td></tr>
<tr><td>low</td><td><?php echo $data[7] ?></td></tr>
<tr><td>volume</td><td><?php echo $data[8] ?></td></tr>
</table>
<?php
fclose ($fp);
?>

In the above script example, the fopen function opens the file to be read. Next, the fgetscv function moves cells from a CSV excel file into an array. The array can then be manipulated as necessary. Lastly, the fclose function closes the the open file.

Altering this code to handle multiple stock ticker symbols in one sheet would be possible, but would make this simple example a bit more complex. This script will however get you started with getting the stock information you need for your PHP web application.

To redirect a page to another page during PHP processing, simply use the header function demonstrated below:

<?php
header(”Location: http://www.victorchen.info”);
?>

Whenever this script is reached, the page will automatically redirect to http://www.victorchen.info.

  1. The header function must occur before any page output. This includes page outputs originating from include() or require() functions. Additionally, keep an eye out for blank lines before the header function is executed.
  2. The URL does not need to be a full url (http://www.victorchen.info). It can alternatively be “../index.html”.

The below example will not work because there is an output of “This will not work” before the header() function is called.

This will not work
<?php
header(”Location: http://www.victorchen.info”);
?>

This example will also not work because the text “This will not work” is placed in an php echo command.

<?php
echo “This will not work”;
header(”Location: http://www.victorchen.info”);
?>

This next example shows how having a hidden space before the first php command will also not work. Please note the first line of the example below is empty! To see this highlight the code below and see the first character is a blank!

<?php
header(”Location: http://www.victorchen.info”);
?>

Lastly, the following code below will work. The string “This will work” is placed after the redirect.

<?php
header(”Location: http://www.victorchen.info”);
echo “This will work”;
?>

Please note that the string “This will work” will never actually execute in this scenario, as the header function will trigger the page to first redirect. Simply placing an if statement around the header function allows toggling of when the header function is executed.

The Web Developer designed for Firefox, Flock and Seamonkey is a free extension that adds a menu and a toolbar. It will run on any platform that these browsers support including Windows, Mac OS X and Linux. Just to list a few features:

  • Disable Javascript
  • Disable Referrer
  • Disable Cookies
  • Add Cookies
  • View Cookie Information
  • Disable Styles
  • Edit Styles (on the fly)
  • Display Form Details
  • Show Passwords
  • Make Form Fields Editable
  • Disable Images
  • Edit HTML (on the fly)
  • Resize Window
  • Various Validation
  • View Generated Source
  • Many other features…

Most of the features are useful for testing various browser compatibilities. Not all users will enable cookies, referrers, images, or javascript on their browser. The Web Developer plugin allows an easy way to test how your site will handle various situations. Most useful if Edit CSS and Edit HTML will bring up windows that allow changes and saves that will update on the fly. This plugin displays as a FireFox toolbar and (like most Firefox plugins) has no problems during the uninstall process.

Overall, Web Developer is a great tool for developing sites in HTML and CSS. It is also a useful tool when developing AJAX enabled sites, as you can view the generated source. To try Web Developer, visit the developer’s site directly at http://chrispederick.com/work/web-developer/ or from Firefox Extensions at https://addons.mozilla.org/en-US/firefox/addon/60.