Exemple #1
0
curl_close($ch);
/*/
$source = '
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:media="http://search.yahoo.com/mrss"
	>

<channel>
	<title>LyricWiki.org</title>
	<atom:link href="http://lyricwiki.wordpress.com/feed/" rel="self" type="application/rss+xml" />
</channel>
</rss>
';
//*/
#echo $source;
$xml = new XmlDocument();
$xml->parse($source);
$items = $xml->getItem("/rss/channel/item[*]");
if ($items) {
    foreach ($items as $item) {
        echo "=================================\n";
        #var_dump($item["TITLE"]);
        echo implode($item["TITLE"][0]) . "\n";
    }
}
//*/
Exemple #2
0
function renderXML($input, $argv, $parser)
{
    // parameters
    $feedURL = $argv["feed"];
    $escapedFeedURL = urlencode($argv["feed"]);
    $maxItems = (int) $argv["maxitems"];
    $addLineFeed = $argv["linefeed"];
    // retrieve the xml source from the cache before trying to fetch it
    // limits possible stress on other people's servers, reduces chance of DOS attacks
    global $wgMemc;
    $cachedSource = false;
    if (debugSwitch("forceload")) {
        $wgMemc->get($escapedFeedURL);
    }
    if (!$cachedSource) {
        // Uses Http::get which is the prefered method to make requests from MediaWiki since it handles going through proxies, etc.
        $timeout = 5;
        // set to zero for no timeout
        $source = Http::get($feedURL, $timeout, array(CURLOPT_FOLLOWLOCATION => 1));
        if (!$source) {
            return wfMsgExt("xml-feedfailed", array('parseinline'));
        }
        // only cache newly fetched sources
        $wgMemc->set($escapedFeedURL, $source, strtotime("+2 hour"));
    } else {
        $source = $cachedSource;
    }
    if (debugSwitch("source")) {
        echo $source . "\n";
    }
    // parse
    $feed = new XmlDocument();
    if (!$feed->parse($source)) {
        return wfMsg("xml-parseerror", $feed->getError());
    }
    // fill in the template with the fields from the xml file
    preg_match_all("#<item path=\"(.*?)\">(.*?)</item>#", filter($input), $matches);
    $result = "";
    foreach ($matches[0] as $i => $text) {
        $path = $matches[1][$i];
        $template = filter(trim(unfilter($matches[2][$i])));
        $items = $feed->getItem($path);
        $count = min(count($items), $maxItems);
        if (!$items) {
            $result .= wfMsg("xml-pathnotfound", $path);
        }
        for ($i = 0; $i < $count; ++$i) {
            $item = $items[$i];
            // fill in the template (use standard template parameter format)
            $text = $template;
            if (preg_match_all("/{{{([a-zA-Z:]*)}}}/", $text, $fields)) {
                foreach (array_unique($fields[1]) as $field) {
                    // SWC 20061113 - Broke the accessing into two lines so that it parses
                    $tempArray = $item[strtoupper($field)];
                    $currValue = implode("", $tempArray[0]);
                    $text = str_replace("{{{{$field}}}}", $currValue, $text);
                }
            }
            // conditially add a line feed to the end of each item
            if ($addLineFeed) {
                $result .= $text . "\n";
            } else {
                $result .= $text;
            }
        }
    }
    return sandboxParse($result);
}