Archive for April, 2008

Web-based applications involve pages connected by links. Therefore, verifying the authenticity of links becomes a crucial factor. For example, one situation may involve verifyng a link before storing it into a database. Pass to the function a url as a string and it will return true (the link works) or false (the link did not work).

public bool checkUrlLink(string url)
{
HttpWebRequest request = HttpWebRequest)WebRequest.Create(url);
request.Proxy = null;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return true;
}
catch
{
return false;
}
}

This code snippet uses HttpWebRequest and HttpWebResponse functions. Therefore, the snippet above requires the following using statement:

using System.Net;

Also note that this code snippet will also check wheather a url is properly formatted. If the format is not correct, it will return false (simply because the url did not receive a response).

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.

Web applications often involve reading from and writing to files local to the web application server. In the case of reading from a file, errors may occur if the proper checks are not in place. One popular way of insuring errors don’t occur during runtime is to check if a file exists before attempting to read the file.

if (File.Exists(”C:\\testfile.txt”))
{
return true;
}
else
{
return false;
}

This method utilizes the Exists function, part of the File class, to check if a file can be found at the specified directory. The string must be reachable from the server on which the web application resides. When expressing the input string, be sure to properly format your directory and file with double back slashes. To use File.Exists, be sure to include the following at the top of your file.

using System.IO;