1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Clase para crear un RSS y asi se suscriban a tu sitio
* Mas informacion de RSS 2.0: http://blogs.law.harvard.edu/tech/rss
* Licencia: GNU/GPL
* NOTA: Puede resultar util la siguiente funcion, http://www.php.net/manual/es/function.date-default-timezone-set.php
*/
class rss2 {
//Variable en la que se almacena el XML
private $cont='';
//Constructor en el que se agrega informacion del RSS del sitio web
/**
* 1) Titulo del sitio web o que se le quiera dar al RSS
* 2) Enlace al sitio que pertenece el RSS
* 3) Descripcion del RSS/Sitio
* 4) Webmaster o Email del webmaster
* 5) Fecha de la ultima publicacion en el RSS, sino se agrega la fecha actual
* 6) Idioma del RSS
* 7) Locale para las fechas puede ser importante
*/
public function __construct($sTitle, $sLink, $sDesc, $sWebmaster, $mLastPubDate=0, $sLang = 'es', $sLocale = 'es_ES') {
@setlocale('LC_ALL',$sLocale);
if(is_numeric($mLastPubDate) && ($mLastPubDate !== 0 || !empty(trim($mLastPubDate)))) $mLastPubDate = date('l, d \d\e F \d\e Y', $mLastPubDate);
else $mLastPubDate = date('l, d \d\e F \d\e Y');
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$xml .= " <rss version=\"2.0\">\n";
$xml .= " <channel>\n";
$xml .= " <title>{$sTitle}</title>\n";
$xml .= " <link>{$sLink}</link>\n";
$xml .= " <description>{$sDesc}</description>\n";
$xml .= " <webmaster>{$sWebmaster}</webmaster>\n";
$xml .= " <language>{$sLang}</language>\n";
$xml .= ' <lastBuildDate>'.date('l, d \d\e F \d\e Y')."</lastBuildDate>\n";
$xml .= " <PubDate>{$mLastPubDate}</PubDate>\n";
$xml .= " <docs>http://blogs.law.harvard.edu/tech/rss</docs>\n";
$this->cont .= utf8_encode($xml);
}
//Constructor
public function rss2($sTitle, $sLink, $sDesc, $sWebmaster, $sLang = 'es', $sLocale = 'es_ES') {
$this->__construct($sTitle, $sLink, $sDesc, $sWebmaster, $sLang, $sLocale);
}
//Agregar una noticia
//Las variables creo que se entienden bien, por si acaso
// son por este orden: Titulo, Enlace a la noticia, Noticia, autor,
// Fecha (puede ser timestamp o pasarle la fecha en texto directamente; si
// se le pasa timestamp usa un patron determinado que puede cambiar en la funcion date),
// categoria (si es un array, pase como valores las categorias a las que pertenece),
// Enlace a los comentarios.
//
// De la fecha en adelante son voluntarios, es decir, fecha, categoria y comentarios
public function add_item($sTitle, $sLink, $sNotice, $sAuthor, $mDate=0, $mCategory='', $sLinkComments='') {
if(is_numeric($mDate)) $mDate = date('l, d \d\e F \d\e Y', $mDate);
$xml = " <item>\n";
$xml .= " <title>{$sTitle}</title>\n";
$xml .= " <link>{$sLink}</link>\n";
$xml .= ' <description><![CDATA['.$sNotice."]]></description>\n";
$xml .= " <pubDate>{$mDate}</pubDate>\n";
$xml .= " <author>{$sAuthor}</author>\n";
if(is_array($mCategory)) {
foreach ($mCategory as $category) {
$xml .= " <category><![CDATA[{$category}]]></category>\n";
}
} elseif(!empty(trim($mCategory))) {
$xml .= " <category><![CDATA[{$category}]]></category>\n";
}
if(!empty(trim($sLinkComments))) $xml .= " <comments>{$sLinkComments}</comments>\n";
$xml .= " </item>\n";
$this->cont .= utf8_encode($xml);
}
//Funcion para que devuelva como un string todo
public function get() {
$xml = " </channel>\n";
$xml .= ' </rss>';
return $this->cont . utf8_encode($xml);
}
//Funcion que devuelve el contenido directamente y
//fuerza el fichero PHP actual como salida RSS
public function output() {
header('Content-type: text/xml; charset=UTF-8', true);
$xml = " </channel>\n";
$xml .= ' </rss>';
echo $this->cont . utf8_encode($xml);
}
}
//Ejemplo de uso
$rss = new rss('Ejemplo RSS','http://blog.zydrick.net/','Ejemplo de uso de la clase RSS', 'ZydRick', time(), 'es', 'es_ES.UTF-8');
//Vamos a poner la noticia en variables
$title = 'Una vieja y un viejo van pal abacete';
$enlace_noticia = 'http://blog.zydrick.net/enlace_a_noticia';
$noticia = 'Una vieja y un viejo van pal abacete,
y en el medio del camino va y se la mete,
va y se la meteeeee...<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/RdTBml4oOZ8"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/RdTBml4oOZ8" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>';
$fecha_noticia = time();
$autor = 'ZydRick';
$categorias = array('canciones','viejos tiempos');
$enlace_comentarios = $enlace_noticia.'#comentarios';
$rss->add_item($title, $enlace_noticia, $noticia, $autor, $fecha_noticia, $categorias, $enlace_comentarios);
$rss->output();