<?php
function GetXMLFirstVal($r,$t) {
if(preg_match_all('/<('.$t.')>(.*)<\/\\1>/Usi',$r,$o)) return $o[2][0];
return false;
}
function GetXMLAllVal($r,$t) {
if(preg_match_all('/<('.$t.')>(.*)<\/\\1>/Usi',$r,$o)) return $o[2];
return array();
}
function GetStream($URL,$mode=0) {
switch($mode) {
case 0:
$results = file_get_contents($URL);
break;
case 1:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $URL);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 0);
curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
$results = curl_exec($ch);
if(curl_error($ch)) {
print_r(curl_getinfo($ch));
exit();
}
break;
case 2:
$info = parse_url($URL);
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
exit();
}
$result = socket_connect($socket, $info['host'], 80);
if ($result < 0) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
}
$in = "GET ".$info['path']." HTTP/1.1\r\n";
$in .= "Host: ".$info['host']."\r\n";
$in .= "Connection: Close\r\n\r\n";
socket_write($socket, $in, strlen($in));
$out = '';
$results = '';
while ($out = socket_read($socket, 2048)) {
$results .= $out;
}
socket_close($socket);
break;
default:
$results = '';
}
return $results;
}
function DisplayRss($RSS,$j)
{
$XML = GetStream($RSS,0);
if($XML) {
$ITEMS = GetXMLAllVal($XML,'item');
$g=0;
foreach($ITEMS AS $ITEM) {
$g++;
$TITLE = GetXMLFirstVal($ITEM,'title');
$LINK = GetXMLFirstVal($ITEM,'link');
$DESC = GetXMLFirstVal($ITEM,'description');
$DATE = GetXMLFirstVal($ITEM,'pubDate');
$CAT = GetXMLFirstVal($ITEM,'category');
if(!$DATE) {
$DATE = GetXMLFirstVal($ITEM,'dc:date');
}
if($DATE) $DATE = strtotime($DATE);
else $DATE = time();
$TITLE = mb_convert_encoding ( $TITLE ,"utf-8","windows-1251");
$DESC = mb_convert_encoding ( $DESC ,"utf-8","windows-1251");
$CAT = mb_convert_encoding ( $CAT ,"utf-8","windows-1251");
$TITLE = html_entity_decode($TITLE,ENT_QUOTES);
$DESC = html_entity_decode($DESC,ENT_QUOTES);
$CAT = html_entity_decode($CAT,ENT_QUOTES);
print("\n <ul><li><a href = '$LINK' target=_blank ><b> $TITLE </b></a>");
$DESC=strip_tags($DESC);
$sl = strlen($DESC);
if($sl>157){
$DESC = str_split($DESC,150);
$DESC = trim($DESC[0]);
}
$DESC= str_replace("<![CDATA[","",$DESC);
$DESC= str_replace("]]>","",$DESC);
echo "".$DESC."...";
echo "</li></ul>";
if ($g==$j){break;}
}
}
}
print("\n");
echo DisplayRss("http://topnews.ru/rss/", 3); //Адрес rss для импорта и сколько выводить новостей
?>