felicitaciones($this->get($dia, $mes)); } function get($dia, $mes) { $this->xmlFile .= $dia.$mes.'.xml'; if (file_exists($this->xmlFile) && filemtime($this->xmlFile) < 86400 + time()) $xml = $this->getFromFile($this->xmlFile); else $xml = $this->getFromUrl('http://edad.anieto2k.com/api.php?d='.$dia.'&m='.$mes); return $this->parseXML($xml); } function getFromFile($xmlFile) { if (!function_exists("file_get_contents")) { $gestor = fopen($xmlFile, "r"); $xml = fread($gestor, filesize($xmlFile)); fclose($gestor); } else { $xml = file_get_contents($xmlFile); } return $xml; } function getFromUrl($url, $cache = true){ if (!function_exists("file_get_contents")) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); $file = curl_exec($ch); curl_close($ch); }else { $file = file_get_contents($url); } if ($cache) $this->saveCache($this->xmlFile, $file); return $file; } function saveCache($xmlFile, $str) { if(!is_writable(dirname($xmlFile))) die("Imposible almacenar el fichero XML, prueba darle permisos de escritura a la carpeta donde se alojan."); $fh = fopen($xmlFile ,"w"); fwrite($fh,$str); fclose($fh); } function parseXML($xml) { // Parseo de XML $doc = new DOMDocument(); $doc->loadXML($xml); $nodes = $doc->getElementsByTagName("user"); $usuarios = array(); if ($nodes->length > 0) { // Hay usuarios for ($i = 0; $i < $nodes->length; $i++) { array_push($usuarios, array( 'id' => $nodes->item($i)->getElementsByTagName('id')->item(0)->nodeValue, 'name' => $nodes->item($i)->getElementsByTagName('nombre')->item(0)->nodeValue, 'url' => $nodes->item($i)->getElementsByTagName('url')->item(0)->nodeValue )); } } return $usuarios; } function felicitaciones($usuarios) { if (!is_array($usuarios) || sizeof($usuarios) == 0) return ''; $salida = ''; return $salida; } } ?>