AMBIL RSS dengan PHP


RSS Feed (Really Simple Syndication) adalah sebuah file berformat XML yang mengijinkan kita untuk berlangganan artikel atau berita dari suatu web. Umum nya hampir setiap web saat ini dilengkapi oleh RSS.

Pada kesempatan ini akan dijelaskan bagaimana memanfaatkan syntax php untuk mengambil informasi atau berita melalui RSS..

hehe :: klo ada yang mau ambil RSS dngan PHP buat webnya :: ya rumayan buat promosi blog ::

__________________________________________________________

<?
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link, $pubDate;
if ($insideitem) {
$tag = $name;
} elseif ($name == “ITEM”) {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $pubDate, $i;
if (!$i) {
$i=1;
}
if (($name == “ITEM”) && ($i<=4)) {
$i++;
printf(“<B><a href=’%s’ class=main target=_blank>%s</a></B><p>%s, %s</p>”,
trim($link),trim($title), substr($pubDate,0,16), $description);
$title = “”;
$description = “”;
$link = “”;
$pubDate=””;
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link, $pubDate;
if ($insideitem) {
switch ($tag) {
case “TITLE”:
$title .= $data;
break;
case “DESCRIPTION”:
$description .= $data;
break;
case “LINK”:
$link .= $data;
break;
case “PUBDATE”:
$pubDate .= $data;
break;
}
}
}

$url = https://diazscript.wordpress.com/feed/rss/;
$insideitem = false;
$tag = “”;
$title = “”;
$description = “”;
$link = “”;
$pubDate =””;
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, “startElement”, “endElement”);
xml_set_character_data_handler($xml_parser, “characterData”);
$fp = fopen($url,”r”);
while ($datarss = fread($fp, 4096))
xml_parse($xml_parser, $datarss, feof($fp)) or die(sprintf(“XML error: %s pada baris %d”,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

?>

__________________________________________________________

klo mau rubah alamatnya tinggal ganti isi variable $url.
tapi ini kuk cuma 2 aja ya …. masi bingung juga ane
smoga bermanfaat

AMBIL RSS dengan PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s