De:PHPStats
Inhaltsverzeichnis
Status unseres Peers mit PHP auslesen
Eine von mir veröffentlichte (experimentelle) PHP-Klasse versucht Auslesen und Fernsteuern des Peer zusammenzufassen. Details dazu hier.
einfache Methode mit preg_match
Der Status unseres Peers kann mit PHP auslesen werden. Hier ein Beispiel wie das ohne die eingebauten XML-Funktionalitäten von PHP genutzt werden kann. Das Beispiel berücksichtigt nicht alle Angaben in Network.xml
$result = @file('http://localhost:8080/Network.xml'); if ($result) { $data = implode('',$result); preg_match('/<peers>(.*?)<active>(.*?)<count>(.*?)<\/count>(.*?)<\/active>(.*?)<\/peers>/is',$data,$matches); $active_peers = $matches[3]; preg_match('/<peers>(.*?)<active>(.*?)<links>(.*?)<\/links>(.*?)<\/active>(.*?)<\/peers>/is',$data,$matches); $active_links = $matches[3]; preg_match('/<peers>(.*?)<active>(.*?)<words>(.*?)<\/words>(.*?)<\/active>(.*?)<\/peers>/is',$data,$matches); $active_words = $matches[3]; preg_match('/<peers>(.*?)<all>(.*?)<count>(.*?)<\/count>(.*?)<\/all>(.*?)<\/peers>/is',$data,$matches); $all_peers = $matches[3]; preg_match('/<peers>(.*?)<all>(.*?)<links>(.*?)<\/links>(.*?)<\/all>(.*?)<\/peers>/is',$data,$matches); $all_links = $matches[3]; preg_match('/<peers>(.*?)<all>(.*?)<words>(.*?)<\/words>(.*?)<\/all>(.*?)<\/peers>/is',$data,$matches); $all_words = $matches[3]; preg_match('/<your>(.*?)<version>(.*?)<\/version>(.*?)<\/your>/is',$data,$matches); $my_version = $matches[2]; preg_match('/<your>(.*?)<links>(.*?)<\/links>(.*?)<\/your>/is',$data,$matches); $my_links = $matches[2]; preg_match('/<your>(.*?)<words>(.*?)<\/words>(.*?)<\/your>/is',$data,$matches); $my_words = $matches[2]; echo 'Im gesamten YaCy-Cluster, bestehend aus insgesamt '. $all_peers.' aktiven und inaktiven Peers, sind aktuell '.$all_links.' Links und '.$all_words. ' Wörter erfasst, hiervon entfallen '.$ac_links.' Links und '.$ac_words.' Wörter auf '. $ac_peers.' direkt erreichbare Peers. Dieser Peer hat momentan '.$my_links.' Links und '. $my_words.' Wörter in seiner lokalen Datenbank und läuft mit YaCy Version '.$my_version.'.'; } else { echo 'Peer offline bzw. nicht erreichbar'; }
bessere Methode mit preg_match
Hierbei werden alle Werte der Network.xml in einen mehrdimensionen Array eingelesen. Anschliessend kann einfach darauf zugegriffen werden.Beispiel hier: $my_version, $my_links und $all_links
$network = xml2array(implode("",@file("http://localhost:8080/Network.xml"))); $my_version = $network['peers'][0]['your'][0]['version']; $my_links = $network['peers'][0]['your'][0]['links']; $all_links = $network['peers'][0]['all'][0]['links']; function xml2array($text) { $reg_exp = '/<(\w+)[^>]*>(.*?)<\/\\1>/s'; preg_match_all($reg_exp, $text, $match); foreach ($match[1] as $key=>$val) { if ( preg_match($reg_exp, $match[2][$key]) ) { $array[$val][] = xml2array($match[2][$key]); } else { $array[$val] = $match[2][$key]; } } return $array; }
Basierend auf Beispielen unter http://de.php.net/xml
komplexere Methode mit xml-Parser
Hierbei werden alle Werte der Network.xml in einen mehrdimensionen Array eingelesen. Dieser muß anschliessend entsprechend ausgewertet werden. Folgendes Beispiel gibt anschliessend nur den Array aus:
$url = 'http://localhost:8080/Network.xml'; $parser = new XMLParser($url); echo "<pr"."e>"; // pre zerlegt für Darstellung im Wiki var_dump($parser->data); echo "</pr"."e>"; // pre zerlegt für Darstellung im Wiki class XMLParser { var $xml_url; var $xml; var $data; function XMLParser($xml_url) { $this->xml_url = $xml_url; $this->xml = xml_parser_create(); xml_set_object($this->xml, $this); xml_set_element_handler($this->xml, 'startHandler', 'endHandler'); xml_set_character_data_handler($this->xml, 'dataHandler'); $this->parse($xml_url); } function parse($xml_url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $xml_url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $store = curl_exec ($ch); $data = curl_exec ($ch); curl_close ($ch); $parse = xml_parse($this->xml, $data, sizeof($data)); if (!$parse) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml)), xml_get_current_line_number($this->xml))); xml_parser_free($this->xml ); } return true; } function startHandler($parser, $name, $attributes) { $data['name'] = $name; if ($attributes) { $data['attributes'] = $attributes; } $this->data[] = $data; } function dataHandler($parser, $data) { if ($data = trim($data)) { $index = count($this->data) - 1; // begin multi-line bug fix (use the .= operator) $this->data[$index]['content'] .= $data; // end multi-line bug fix } } function endHandler($parser, $name) { if (count($this->data) > 1) { $data = array_pop($this->data); $index = count($this->data) - 1; $this->data[$index]['child'][] = $data; } } }
Dieses Beispiel liefert anschliessend folgende Ausgabe:
array(1) { [0]=> array(2) { ["name"]=> string(5) "PEERS" ["child"]=> array(6) { [0]=> array(2) { ["name"]=> string(6) "ACTIVE" ["child"]=> array(3) { [0]=> array(2) { ["name"]=> string(5) "COUNT" ["content"]=> string(2) "53" } [1]=> array(2) { ["name"]=> string(5) "LINKS" ["content"]=> string(11) "147.586.635" } [2]=> array(2) { ["name"]=> string(5) "WORDS" ["content"]=> string(11) "106.611.300" } } } [1]=> array(2) { ["name"]=> string(7) "PASSIVE" ["child"]=> array(3) { [0]=> array(2) { ["name"]=> string(5) "COUNT" ["content"]=> string(3) "432" } [1]=> array(2) { ["name"]=> string(5) "LINKS" ["content"]=> string(11) "213.220.807" } [2]=> array(2) { ["name"]=> string(5) "WORDS" ["content"]=> string(11) "175.320.343" } } } [2]=> array(2) { ["name"]=> string(9) "POTENTIAL" ["child"]=> array(3) { [0]=> array(2) { ["name"]=> string(5) "COUNT" ["content"]=> string(3) "179" } [1]=> array(2) { ["name"]=> string(5) "LINKS" ["content"]=> string(10) "12.696.529" } [2]=> array(2) { ["name"]=> string(5) "WORDS" ["content"]=> string(10) "16.369.091" } } } [3]=> array(2) { ["name"]=> string(3) "ALL" ["child"]=> array(3) { [0]=> array(2) { ["name"]=> string(5) "COUNT" ["content"]=> string(3) "664" } [1]=> array(2) { ["name"]=> string(5) "LINKS" ["content"]=> string(11) "373.503.971" } [2]=> array(2) { ["name"]=> string(5) "WORDS" ["content"]=> string(11) "298.300.734" } } } [4]=> array(2) { ["name"]=> string(4) "YOUR" ["child"]=> array(16) { [0]=> array(2) { ["name"]=> string(4) "NAME" ["content"]=> string(5) "angel" } [1]=> array(2) { ["name"]=> string(4) "TYPE" ["content"]=> string(9) "principal" } [2]=> array(2) { ["name"]=> string(7) "VERSION" ["content"]=> string(9) "0.4510205" } [3]=> array(2) { ["name"]=> string(3) "UTC" ["content"]=> string(5) "+0200" } [4]=> array(2) { ["name"]=> string(6) "UPTIME" ["content"]=> string(11) "1 day 20:38" } [5]=> array(2) { ["name"]=> string(5) "LINKS" ["content"]=> string(9) "9.610.207" } [6]=> array(2) { ["name"]=> string(5) "WORDS" ["content"]=> string(9) "4.478.128" } [7]=> array(1) { ["name"]=> string(11) "ACCEPTCRAWL" } [8]=> array(2) { ["name"]=> string(11) "ACCEPTINDEX" ["content"]=> string(1) "1" } [9]=> array(2) { ["name"]=> string(9) "SENTWORDS" ["content"]=> string(10) "10.170.649" } [10]=> array(2) { ["name"]=> string(8) "SENTURLS" ["content"]=> string(9) "4.551.093" } [11]=> array(2) { ["name"]=> string(13) "RECEIVEDWORDS" ["content"]=> string(10) "20.997.306" } [12]=> array(2) { ["name"]=> string(12) "RECEIVEDURLS" ["content"]=> string(9) "2.347.634" } [13]=> array(2) { ["name"]=> string(3) "PPM" ["content"]=> string(1) "2" } [14]=> array(2) { ["name"]=> string(5) "SEEDS" ["content"]=> string(2) "52" } [15]=> array(2) { ["name"]=> string(8) "CONNECTS" ["content"]=> string(2) "14" } } } [5]=> array(1) { ["name"]=> string(6) "STATUS" } } } }
Basiert auf Beispielen unter http://de.php.net/xml und http://www.digital-seven.net/?option=com_content&task=view&id=69