Writing Command-Line PHP Programs

A simple PHP program that outputs data works fine from the command line. Con­sider Example 19-1, which uses the Yahoo! Weather API to print out the current weather conditions for a zip code.

Example 19-1. Finding the weather

// Zip code to look up weather for

$zip = “98052”;

// YQL query to find the weather

// See https://developer.yahoo.com/weather/ for more info

$yqt = ‘select item.condition from weather.forecast where woeid in ‘.

   ‘(select woeid from geo.places(1) where text=”‘.$zip.'”)’;

 

// The params that the Yahoo! YQL query endpoint expects

$params = array(“q” => $yql,

  “format” => “json”,

  “env” => “store://datatables.org/alltableswithkeys”);

// Build the YQL URL, appending the query parameters

$url = “https://query.yahooapis.com/v1/public/yql?” . http_build_query($params);

// Make the request

$response = file_get_contents($url);

// Decode the response as JSON $json = json_decode($response);

// Select the object in the nested JSON response that contains the info

$conditions = $json->query->results->channel->item->condition;

// Print out the weather

print “At {$conditions->date} it is {$conditions->temp} degrees ” .

 “and {$conditions->text} in $zip\n”;

If you save Example 19-1 in a file called weather.php, you can run it with a command such as php weather.php and be told the current weather. But it’s only accurate if you want the weather for zip code 98052. Otherwise you have to edit the file. This is not very useful. It would be better if you could provide a zip code as an argument to the program when you run it. Example 19-2 is an updated version of the program that looks in the $_SERVER[‘argv’] array for command-line arguments. The command-line version of the PHP engine automatically populates this array with provided arguments.

Example 19-2. Accessing command-line arguments

// Zip code to look up weather for

if (isset($_SERVER[‘argv’][ ])) {

$zip = $_SERVER[‘argv’][ ];

} else {

print “Please specify a zip code.\n”;

exit();

}

// YQL query to find the weather

// See https://developer.yahoo.com/weather/ for more info

$yql = ‘select item.condition from weather-forecast where woeid in ‘ .

  ‘(select woeid from geo.places(l) where text=”‘.$zip.'”)’;

 

// The params that the Yahoo! YQL query endpoint expects

$params = array(“q” => $yql,

  “format” => “json”,

  “env” => “store://datatables.org/alltableswithkeys”);

 

// Build the YQL URL, appending the query parameters

$url = “https://query.yahooapis.com/v1/public/yql?” . http_build_query($params);

// Make the request

$response = file_get_contents($url);

// Decode the response as JSON

$json = json_decode($response);

// Select the object in the nested JSON response that contains the info

$conditions = $json->query->results->channel->item->condition;

// Print out the weather

print “At {$conditions->date} it is {$conditions->temp} degrees ” .

 “and {$conditions->text} in $zip\n”;

Assuming you save Example 19-2 in weather2.php, you can run php weather2 19096 to get the weather for zip code 19096.

Note that the first argument is at $_SERVER[‘argv’][1], even though, as mentioned in “Creating a Numeric Array” on page 60, PHP arrays start with index 0. This is because $_SERVER[‘argv’][0] contains the name of the program you’ve run. In the case of running php weather2.php 19096,$_SERVER[‘argv’][0] is weather2.php.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *