#!D:\PHP\php.exe
<?php
error_reporting(E_ERROR);
// Set time limit to indefinite execution
set_time_limit (0);
// Set the ip and port we will listen on
$address = '192.168.0.2';
$port = 9000;
// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
socket_listen($sock);
$i=0;
while(true){
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024 bytes
$input = socket_read($client, 1024);
// Create Input
$output = get_weather("1");
// Display output back to client
socket_write($client, $output);
// Close the client (child) socket
socket_close($client);
}
// Close the master sockets
socket_close($sock);
//##############################################
// Begin Function and Classes
//##############################################
function get_weather($which_grab){
$objXML = new xml2Array();
$xml = file_get_contents("http://www.wunderground.com/auto/rss_full/NC/Morehead_City.xml");
$arrOutput = $objXML->parse($xml);
$date_of_pull = $arrOutput[0][children][0][children][9][children][0][tagData];
list($temp_hold, $humidity_hold, $pressure_hold, $cond_hold, $winddir_hold, $windspeed_hold) = split('\|', $arrOutput[0][children][0][children][9][children][2][tagData]);
$temp_get = split(':', $temp_hold);
$humidity_get = split(':', $humidity_hold);
$pressure_get = split(':', $pressure_hold);
$cond_get = split(':', $cond_hold);
$winddir_get = split(':', $winddir_hold);
$windspeed_get = split(':', $windspeed_hold);
$date_alone = split('-', $date_of_pull);
$temp_all = split('\/', $temp_get[1]);
$temp = preg_replace('/\W/', '', $temp_all[0]);
$humidity = str_replace(' ', '', $humidity_get[1]);
$pressure = str_replace(' ', '', $pressure_get[1]);
$cond = str_replace(' ', '', $cond_get[1]);
$winddir = str_replace(' ', '', $winddir_get[1]);
$windspeed = str_replace(' ', '', $windspeed_get[1]);
$date = $date_alone[1];
switch($cond){
case "Overcast":
$cond_num = "1";
}
if($which_grab == "1"){
$short_send = "$date::$temp::$cond::$humidity::";
echo $short_send;
return $short_send;
}else{
$full_send = "$temp::$humidity::$pressure::$cond_num::$winddir::$windspeed\n";
return $full_send;
}
//foreach($arrOutput[0][children][0][children][9][children][2] as $v=>$k){
// echo "$v-$k<br>";
//}
//print_r($arrOutput); //print it out, or do whatever!
}
class xml2Array {
var $arrOutput = array();
var $resParser;
var $strXmlData;
function parse($strInputXML) {
$this->resParser = xml_parser_create ();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->resParser, "tagData");
$this->strXmlData = xml_parse($this->resParser,$strInputXML );
if(!$this->strXmlData) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser)));
}
xml_parser_free($this->resParser);
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
$tag=array("name"=>$name,"attrs"=>$attrs);
array_push($this->arrOutput,$tag);
}
function tagData($parser, $tagData) {
if(trim($tagData)) {
if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
}
else {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
}
}
}
function tagClosed($parser, $name) {
$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
array_pop($this->arrOutput);
}
}
?>
I want this to be an executable so I can create a background sevice that runs with this port open.
I have tried windows inistsrv.exe to try and create a service, I have tried apollo to compile the script into an executable but that didn't seem to work very well.
Any ideas on how I can use this script?