Rumahweb's News, Article, and Tutorial of Web Development

CURL dan Rss Feed Reader

Penggunaan CURL (Client URL Library) merupakan opsi terbaik yang dapat digunakan ketika setting allow_url_fopen diset off.

Curl merupakan salah satu library yang sudah didukung penggunaannya oleh PHP. Referensi mengenai Curl dan PHP : http://us2.php.net/curl

allow_url_fopen Off
Yap, ketika setting allow_url_fopen diset off, maka
[php]
fopen (“http://namadomain.com/path/ke/rssfeeds/”,”r”);
[/php]
tidak akan bisa digunakan untuk membuka dan membaca Rss Feed dari URL lain. Lalu bagaimana cara mengakalinya ?

Gunakan CURL, dan berikut code snippet untuk membaca Rss Feed.
[php]
//beri nama rss.function.php
$channel = array();
$defwrite = “”;
$main = “”;
$min = 0;
function awal($parser, $nama, $attrb) {
global $channel, $defwrite, $main;
if($nama == “RSS” || $nama == “RDF:RDF” || $nama == “ITEMS”){
$defwrite = “”;
} elseif($nama == “CHANNEL”){
$main = “CHANNEL”;
} elseif ($nama == “IMAGE”){
$main = “IMAGE”;
$channel[“IMAGE”] = array();
} elseif ($nama == “ITEM”){
$main = “ITEMS”;
} else {
$defwrite = $nama;
}
}

function akhir($parser, $nama) {
global $channel, $defwrite, $min;
$defwrite = “”;
if ($nama == “ITEM”) {
$min++;
}
}

function karakter($parser, $data) {
global $channel, $defwrite, $main, $min;
if ($defwrite != “”) {
if ($main == “CHANNEL”){
if (isset($channel[$defwrite])){
$channel[$defwrite] .= $data;
} else {
$channel[$defwrite] = $data;
}
} elseif ($main == “IMAGE”){
if (isset($channel[$main][$defwrite])){
$channel[$main][$defwrite] .= $data;
} else {
$channel[$main][$defwrite] = $data;
}
} elseif ($main == “ITEMS”){
if (isset($channel[$main][$min][$defwrite])){
$channel[$main][$min][$defwrite] .= $data;
} else {
$channel[$main][$min][$defwrite] = $data;
}
}
}
}

function parse($url){
$runparser = xml_parser_create();
xml_set_element_handler($runparser, “awal”, “akhir”);
xml_set_character_data_handler($runparser, “karakter”);

$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$output = curl_exec($ch);
curl_close($ch);

xml_parse_into_struct($runparser, $output, $vals, $index);
xml_parser_free($runparser);

}
[/php]

Dibawah ini adalah cara untuk menggunakan function diatas:
[php]
//include dengan relative path
include (“rss.function.php”);
//baca rss feed pada rumahweb.com/journal
parse(‘http://www.rumahweb.com/journal/feed/’);
//maksimal feed yang dibaca
$max = 10;
if (isset($channel[“ITEMS”])) {
if (count($channel[“ITEMS”]) > 0) {
for($i = 0;$i < $max;$i++) { if (isset($channel["ITEMS"][$i]["LINK"])) { echo "

“.$channel[“ITEMS”][$i][“TITLE”].”“;
} else {
echo “

“.$channel[“ITEMS”][$i][“TITLE”].”

“;
}
echo $channel[“ITEMS”][$i][“DESCRIPTION”];
}
} else {
print (“Feed tidak ditemukan”);
}
}
[/php]

Hasilnya, dapat dilihat di sini

Selamat Mencoba.. šŸ™‚

Tinggalkan Komentar

Rumahweb

1 comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog Competition