Web Development
Using PHP

Simpson College
Computer Science

PHP Command Line

Setup PHP for the command line

By default, you can run PHP from the command line in most UNIX setups. Unfortunately, by default, windows installs of php don't usually get set up for the command prompt. It is necessary to add the php.exe to the command line. Here is a lot of information on running PHP via the command line:
http://php.net/manual/en/install.windows.commandline.php

Note that our PHP install is in a different directory than the above page assumes.

Many of these commands require proper permission to execute, and there may be a problem on lab computers getting them to work.

A very useful function to use in PHP scripts: file_get_contents. This command can put the contents of a file, or even a document fetched over HTTP into a variable.

However, having everything in one variable is a bit inconvenient. It will be easier to process if the file contents are split into an array, with each array location being a different line in the file. This can be done with the explode command.

Example, fetching weather data:

<?php
# This is a PHP script. It is designed to be run from the command line.
# It is possible to include this in a web page script. Remember though, the user
# will wait for your web page, which will wait for kcci's web page. It
# is a slow process. Plus, it decreases reliability because you now have
# more things that could go wrong.

# Print that we are fetching a page
print "Fetching page\n";

# Fetch the page
$contents = file_get_contents('http://www.schoolnet8.com/cc.phtml');

# Rather than 1 chunk of text, split the text apart into an array based on line feeds
$contents_array = explode("\n",$contents);

# For each line in the file:
foreach($contents_array as $line) {

	# Run this regular exression.
	if (preg_match("/([A-Za-z ]*)<\/a><\/th><td >([0-9: APM]*)<\/td><td>([0-9]*)/", $line, $matches)) {
		# Ok, the regular expression matched.
		# Array 0 of $matches will have the whole match
		# Array 1 the first parenthesized match, Array 2 the second, etc.
		echo "$matches[1]\n";
		echo " Time: $matches[2]\n";
		echo " Temp: $matches[3]\n";
	}
}
# We are done.
echo "\nDone fetching page.";
?>

Wait, should we even be using regex here? Read this article and then this article.

The proper way to do this is actually parse the HTML. HTML parsers are build into PHP so this is easy. Use the domDocument class to parse and access the HTML document.

<?php
# This is a PHP script. It is designed to be run from the command line.
# It is possible to include this in a web page script. Remember though, the user
# will wait for your web page, which will wait for kcci's web page. It
# is a slow process. Plus, it decreases reliability because you now have
# more things that could go wrong.

# Print that we are fetcing a page
print "Fetching page\n";

# Fetch the page
$contents = file_get_contents('http://www.schoolnet8.com/cc.phtml');

# Create a new Document Object Model DOM
$dom = new domDocument;

# Load HTML into the DOM
$dom->loadHTML($contents);

# Get rid of excess whitespace
$dom->preserveWhiteSpace = false;

# Get the table object
$tables = $dom->getElementsByTagName('table');

# Get all rows from the table
$rows = $tables->item(0)->getElementsByTagName('tr');

# Loop each row
foreach ($rows as $row)
{
	# Get each column marked by <td>
	$cols = $row->getElementsByTagName('td');

	# The city is marked by a table header <td> tag. Get that.
	$headers = $row->getElementsByTagName('th');

	# Print out what we got
	echo $headers->item(0)->nodeValue;
	echo ' '.$cols->item(0)->nodeValue;
	echo "\n";
	echo ' '.$cols->item(1)->nodeValue;
	echo "\n";
	echo ' '.$cols->item(2)->nodeValue;
	echo "\n";
}
echo "\nDone fetching page";
?>

Setting up scheduled tasks

Setting up scheduled tasks

There are many ways to set up scheduled tasks. Two popular ways:

UNIX: Use a cron job.

Windows: Use a scheduled task.

You are not logged in. Log in here and track your progress.