{ 2007 10 07 }
AirNow Web Page Scraper
<?php
/*
AIRNow Web Page Scraper
Language: PHP
Reads a web page and returns one line from it.
*/
// Define variables:
// url of the page with the air quality index data for New York City:
$url =
'http://airnow.gov/index.cfm?action=airnow.showlocal&cityid=164';
// open the file at the URL for reading:
$filePath = fopen ($url, "r");
// as long as you haven't reached the end of the file:
while (!feof($filePath))
{
// read one line at a time, and strip all HTML and
// PHP tags from the line:
$line = fgetss($filePath, 4096);
// if the previous line was the "observed at line" preceding
// the particle matter reading, then $readParticles = 1 and
// you should get this line, trim everything but the number,
// and save the result in $particles:
if ($readParticles == 1) {
$particles = trim($line);
echo "< AQI: $particles>";
$readParticles = 0;
}
// if the current line contains the substring "AQI observed at"
// then the line following it is either the particle reading
// or the ozone reading:
if (preg_match('/AQI observed at /', $line)) {
// if $particles == -1, you haven't gotten
// a value for it yet, so the next line
// will be the particle value:
if ($particles == -1) {
$readParticles = 1;
}
}
}
// close the file at the URL, you're done:
fclose($filePath);
?>