Beste,
Ik wil graag een xml uitlezen die bepaalde info gaat toevoegen aan de datebase.
Zou iemand mij kunnen helpen.
De xml heeft namelijk meerdere teksten en er moet maar 1 regel opgeslagen worden.
Dit is de opbouw van het xml bestand.
De info pointer moet hij vervolgens opslaan in de datebase.Code:<HOSTNAME>tekst</HOSTNAME> <USERAGENT>tekst</USERAGENT> <UNDERRUNS>tekst</UNDERRUNS> <CONNECTTIME>tekst</CONNECTTIME> <POINTER>tekst</POINTER> <UID>tekst</UID>
Het xml bestand wordt wel uitgelezen uit een externe link.
- xml uitlezen en toevoegen aan datebase.
-
12-12-2009, 14:12 #1
- Berichten
- 697
- Lid sinds
- 17 Jaar
xml uitlezen en toevoegen aan datebase.
-
In de schijnwerper
Websitedown.nl - Controleren of jou of andere websites online of offline zijnWebsite te koopwebshop in laadkabels/laadpalen (sinds 2019) incl leveranciersWebsite te koopAutoriteit links aangeboden | Hoge DR & DA + Duizenden bezoekers. Alle NICHES vrijwelOverige dealsOpzoek naar extra werkzaamheden tarief -> €18,50 per uurFreelance / Werk -
12-12-2009, 14:39 #2
- Berichten
- 613
- Lid sinds
- 19 Jaar
Met simplexml kun je het bestand uitlezen en vervolgens verder verwerken.
-
12-12-2009, 14:44 #3
- Berichten
- 697
- Lid sinds
- 17 Jaar
Ja een deel wordt al uitgelezen, het gaat om een shoutcast script.
De class is aanwezig alleen is die erg lang om hier te plaatsen.
-
12-12-2009, 15:01 #4
- Berichten
- 49
- Lid sinds
- 15 Jaar
Misschien is het handig om ook even te vermelden of het een web- of desktop applicatie moet zijn en voor welke server-side scripttaal (PHP, ASP of iets anders?).
-
12-12-2009, 15:06 #5
- Berichten
- 697
- Lid sinds
- 17 Jaar
Ik zal toch maar even de class plaatsen hier, misschien is dat een beter idee.
Op deze line leest hij volgens mij de xml bestand van shoutcast uit:
PHP Code:$d = file_get_contents("http://{$serverData['domain']}:{$serverData['port']}/admin.cgi?mode=viewxml&page=3&pass={$serverData['admPass']}");
Code:<?xml version="1.0" standalone="yes" ?> <!DOCTYPE SHOUTCASTSERVER (View Source for full doctype...)> - <SHOUTCASTSERVER> - <LISTENERS> - <LISTENER> <HOSTNAME>IPADRESS</HOSTNAME> <USERAGENT>IJAFCEIAIFFAEBHBHJEFEJDDGIJDBFCGFICYEJG</USERAGENT> <UNDERRUNS>0</UNDERRUNS> <CONNECTTIME>25</CONNECTTIME> <POINTER>0</POINTER> <UID>14274</UID> </LISTENER> </LISTENERS> </SHOUTCASTSERVER>
PHP Code:<?php
class listeners
{
//------------------------------------------------------------
// PROPERTIES
//------------------------------------------------------------
//--Protected properties--//
/**
* The server details
* @var array
* @access protected
*/
protected $_feedsData = array();
/**
* The database connection
* @var object
* @access protected
*/
protected $_dbConn;
/**
* The database table
* @var string
* @access protected
*/
protected $_dbTable;
/**
* The timestamp
* @var int
* @access protected
*/
protected $_timestamp;
//------------------------------------------------------------
// METHODS
//------------------------------------------------------------
//--Public methods--//
/**
* This is the class constructor.
* It connect to DB
* @param array $dbData array('host'=>'localhost', 'user'=>'abc123', 'pass'=>'abc123', 'dbName'=>'testdb', 'dbTable'=>'testtable')
* @access public
* @return void
*/
public function __construct($dbData)
{
//Connect to DB
$this->_dbConn = mysql_connect($dbData['host'], $dbData['user'], $dbData['pass']) or die(mysql_error());
mysql_select_db($dbData['dbName'], $this->_dbConn);
$this->_dbTable = $dbData['dbTable'];
//Set connection to UTF8
if (!mysql_query("SET NAMES utf8", $this->_dbConn)) {
die('Error set char : ' . mysql_error($this->_dbConn));
}
//Set timestamp
$this->_timestamp = time();
}
// --------------------------------------------------------------------
/**
* Locate IP with IPInfoDB
* @param array $feedData array('domain'=>'example.com', 'port'=>'9999', 'admPass'=>'abc123', 'feedName'=>'feed123')
* @access public
* @return array The geolocation data
*/
public function setFeed($feedData)
{
$this->_feedsData[] = $feedData;
}
// --------------------------------------------------------------------
/**
* Update listeners
* @access public
* @return void
*/
public function updateListeners() {
if (is_array($this->_feedsData)) {
foreach ($this->_feedsData as $feedData) {
//Get listeners
$listeners = $this->_listenersIP($feedData);
$ips = array();
for ($i=0;$i<sizeof($listeners);$i++) {
$ip = $listeners[$i];
//Check if listener already in db
$sql = mysql_query("SELECT * FROM {$this->_dbTable} WHERE `ip_address_int` = INET_ATON('$ip') AND `feed` = '{$feedData['feedName']}' LIMIT 1", $this->_dbConn);
if (mysql_num_rows($sql)) {
$data = mysql_fetch_array($sql);
$id = $data['id'];
$active = $data['active'];
if ($active) {
$updateSql = "UPDATE {$this->_dbTable} SET `timestamp_last_online` = '{$this->_timestamp}' WHERE `id` = '$id'";
} else {
$updateSql = "UPDATE {$this->_dbTable} SET `timestamp_last_online` = '{$this->_timestamp}', `timestamp` = '{$this->_timestamp}', `active` = '1' WHERE `id` = '$id'";
}
//run
if (!mysql_query($updateSql, $this->_dbConn)) {
die('Error updateListeners : ' . mysql_error($this->_dbConn));
}
} else {
//Not in DB, add the IP to the array of IPs to geolocate
$ips[] = $ip;
}
}
//Insert new listeners
if (is_array($ips) && !empty($ips)) {
$this->_insertListeners($ips, $feedData['feedName']);
}
}
}
}
// --------------------------------------------------------------------
/**
* Add new listeners to DB
* @access public
* @return void
*/
public function setInactive() {
$updateSql = "UPDATE `{$this->_dbTable}` set `active` = '0' WHERE `timestamp_last_online` < '$this->_timestamp' AND `active` = '1'";
if (!mysql_query($updateSql, $this->_dbConn)) {
die('Error setInactive : ' . mysql_error($this->_dbConn));
}
}
// --------------------------------------------------------------------
/**
* Delete inactive listeners older than 7 days
* @access public
* @return void
*/
public function deleteOld() {
$lastweek = $this->_timestamp - 3600*24*7;
$deleteSql = "DELETE FROM `{$this->_dbTable}` WHERE `timestamp` < '$lastweek'";
if (!mysql_query($deleteSql, $this->_dbConn)) {
die('Error deleteOld : ' . mysql_error($this->_dbConn));
}
}
// --------------------------------------------------------------------
//--protected methods--//
/**
* Locate IP with IPInfoDB
* @param array $ips The IP addresses
* @access protected
* @return array The geolocation data
*/
protected function _geolocateIps($ips)
{
//Separate all IPs with a comma
$ipsCs = implode(",", $ips);
//Connect to IPInfoDB
$d = @file_get_contents("http://ipinfodb.com/ip_query2.php?ip=$ipsCs");
//Use backup server if cannot make a connection
if (!$d){
$backup = file_get_contents("http://backup.ipinfodb.com/ip_query2.php?ip=$ipsCs");
$answer = new SimpleXMLElement($backup);
if (!$backup) return false; // Failed to open connection
}else{
$answer = new SimpleXMLElement($d);
}
//var_dump($answer->Location);
return $this->_objectToArray($answer);
}
// --------------------------------------------------------------------
/**
* Get the listeners IP
* @param array $serverData The server details
* @access protected
* @return array The listeners array
*/
protected function _listenersIP($serverData) {
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10');
$d = file_get_contents("http://{$serverData['domain']}:{$serverData['port']}/admin.cgi?mode=viewxml&page=3&pass={$serverData['admPass']}");
if (!$d) {
return false; // Failed to open connection
}
//Transform to object
$shoutcastXml = new SimpleXMLElement($d);
//Get IP addresses of listeners
if(!empty($shoutcastXml->LISTENERS->LISTENER)) {
foreach ($shoutcastXml->LISTENERS->LISTENER as $listener) {
$ips[] = (string)$listener->HOSTNAME;
}
}
return $ips;
}
// --------------------------------------------------------------------
/**
* Add new listeners to DB
* @param array $ips The ips of new listeners
* @param string $feedName The feed name
* @access protected
* @return void
*/
protected function _insertListeners($ips, $feedName) {
$k = 0;
$ipsSplit = array();
//Split IP array by 25
for($i=0;$i<count($ips);$i++){
if (!(($i+1) % 25)) $k++;
$ipsSplit[$k][] = $ips[$i];
}
//Geolocate IPs by 25 and build the insert SQL
for ($i=0;$i<count($ipsSplit);$i++) {
if (count($ipsSplit[$i])) {
$ipsData = $this->_geolocateIps($ipsSplit[$i]);
foreach ($ipsData as $ipData) {
$insertSql = "INSERT INTO `{$this->_dbTable}` (`ip_address_int`, `ip_address`, `country_code`, `country_name`, `region_name`, `city`, `latitude`, `longitude`, `timezone`, `feed`, `active`, `timestamp`, `timestamp_last_online`) VALUES (INET_ATON('{$ipData['Ip']}'), '{$ipData['Ip']}', '{$ipData['CountryCode']}', '{$ipData['CountryName']}', '{$ipData['RegionName']}', '{$ipData['City']}', '{$ipData['Latitude']}', '{$ipData['Longitude']}', '{$ipData['Timezone']}', '{$feedName}', '1', '{$this->_timestamp}', '{$this->_timestamp}')";
if (!mysql_query($insertSql, $this->_dbConn)) {
die('Error: ' . mysql_error($this->_dbConn));
}
}
}
}
}
// --------------------------------------------------------------------
/**
* Transform the XML location object into an array
* @param object $object The object to transform
* @access protected
* @return array The array
*/
protected function _objectToArray($object) {
$i = 0;
foreach($object->Location as $key => $ipData) {
foreach($ipData as $field => $val) {
$ipArr[$i][(string)$field] = mysql_real_escape_string((string)$val, $this->_dbConn);
}
$i++;
}
return $ipArr;
}
}
?>Laatst aangepast door R Den Boef : 12-12-2009 om 15:11
Plaats een
- + Advertentie
- + Onderwerp
Marktplaats
Webmasterforum
- Websites algemeen
- Sitechecks
- Marketing
- Domeinen algemeen
- Waardebepaling
- CMS
- Wordpress
- Joomla
- Magento
- Google algemeen
- SEO
- Analytics
- Adsense
- Adwords
- HTML / XHTML
- CSS
- Programmeren
- PHP
- Javascript
- JQuery
- MySQL
- Ondernemen algemeen
- Belastingen
- Juridisch
- Grafisch ontwerp
- Hosting Algemeen
- Hardware Info
- Offtopic