As work is continuing on the script, it seemed appropriate to move it to its own thread. The latest version of the script can be obtained below.
- Code: Select all
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
//======================================================================
// Update Time Tool 1.1 - Written by Lordieth
//======================================================================
//
//I take no responsibility for the use (or misuse) of this script. Use at your own risk.
ini_set("user_agent","Nation update time checker, ran by [nation] - see https://forum.nationstates.net/viewtopic.php?f=15&t=345458");
//set your timezone here to get the correct returned update times
date_default_timezone_set("Europe/London");
//here we'll store any errors
$errors = array();
//this is where we'll store the results, if any
$results = array();
if(isset($_POST['submit'])){
//print_r($_POST['nation']);
if(empty($_POST['nation'][0])){
$errors[] = "Nation 1 has not been entered";
}
if(empty($_POST['nation'][1])){
$errors[] = "Nation 2 has not been entered";
}
if(empty($_POST['nation'][2])){
$errors[] = "Nation 3 has not been entered";
}
if(empty($_POST['nation'][3])){
$errors[] = "Nation 4 has not been entered";
}
if(empty($errors)){
//no errors, now we can run.
$i = 0;
$j = 0;
$events = array();
$array = array();
foreach($_POST['nation'] as $nation){
//replace spaces with underscores to make nation name url-friendly
$nation = str_replace(" ", "_", trim($nation));
//stores the xml contents in $page, if the nation exists
$page = @file_get_contents("http://www.nationstates.net/cgi-bin/api.cgi?nation=".trim($nation)."&q=happenings+region");
//var_dump($http_response_header);
//check response from file_get_contents that the nation exists
if(strpos($http_response_header[0], "HTTP/1.1 404 Not Found") === false){
$xml = simplexml_load_string($page);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
//delay timer. Comment out or set to 0 if you want the script to run faster
sleep(5);
}
else{
$errors[] = "API has returned a 'HTTP/1.1 404 Not Found' response for ".$nation." <br>";
$array = array(); // empty the array
}
//echo "<pre>";
//print_r($array);
//echo "</pre>";
//die();
if($array){
$events = $array['HAPPENINGS']['EVENT'];
$events['REGION'] = $array['REGION'];
//echo "<pre>";
//print_r($events);
//echo "</pre>";
//die();
}
else{
$errors[] = $nation. " could not be found.<br>";
}
//print_r($events);
//die();
if($events){ // if any events have been found
$region = $events['REGION']; //store the region this nation belongs to
foreach($events as $event){
//echo "<pre>";
//print_r($event);
//echo "</pre>";
//echo $event['TEXT']."<br>";
// store the shards for the nations in an array that have a "Following new legislation..." string in the XML
// We break out of the loop on the first legislation it finds for that nation
if(@strpos($event['TEXT'], "Following new legislation") !== false){
$results[$i]['REGION'] = "<a href='http://www.nationstates.net/region=".$region."' target='_blank'>$region</a>";
$results[$i]['TEXT'] = preg_replace("/@@(\S+)@@/", "<a href='http://www.nationstates.net/nation=$1' target='_blank'>$1</a>", $event['TEXT']);
$results[$i]['TIMESTAMP'] = $event['TIMESTAMP'];
$i++;
break;
}
}
}
}
}
}
if($errors){
foreach($errors as $error){
echo $error."<br>";
}
echo "<br>";
}
?>
<form action="index.php" method="post">
Nation 1: <input type="text" name="nation[]"><br>
Nation 2: <input type="text" name="nation[]"><br>
Nation 3: <input type="text" name="nation[]"><br>
Nation 4: <input type="text" name="nation[]"><br>
<input type="submit" name ="submit" value="Submit">
</form>
<?php
if($results){
//echo "<pre>";
//print_r($results);
//echo "</pre>";
//sorts the $results array by date
usort($results, 'date_compare');
//echo "<pre>";
//print_r($results);
//echo "</pre>";
//die();
//loops through $results away and prints out return nation data
foreach($results as $result){
echo $result['REGION']."<br>";
echo date("D M j G:i:s", $result['TIMESTAMP'])."<br>";
echo $result['TEXT']."<br>";
}
echo "<br><br>";
}
?>
</body>
</html>
<?php
// function to sort dates from earliest to latest
function date_compare($a, $b)
{
return $a['TIMESTAMP'] < $b['TIMESTAMP'];
}
?>