3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Parser { // main public static function parse($xmlData) { $res = null; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->recover = true; if(!$dom->loadXML($xmlData)) { $res['error'] = libxml_get_errors(); libxml_clear_errors(); } else { self::read($res, $dom); } return $res; } // tools // TODO use alternate class private static function parseText($str) { return trim(htmlentities($str)); } // void private static function read(&$res, &$dom) { $rss = $dom->getElementsByTagName('rss'); if($rss->length == 0) { $res['error'] = 'No rss node.'; } else { $channel = $rss->item(0)->getElementsByTagName('channel'); if($channel->length == 0) { $res['error'] = 'No channel node.'; } else { self::getNodes($res, $channel, array('title', 'link', 'description')); } } } private static function getNode(&$res, &$channel, $name) { $node = $channel->item(0)->getElementsByTagName($name); if($node->length == 0) { $res[$name] = null; } else { $res[$name] = self::parseText($node->item(0)->nodeValue); } } private static function getNodes(&$res, &$channel, $names) { foreach($names as $name) { self::getNode($res, $channel, $name); } } } $file = file_get_contents('http://korben.info/feed'); if(!$file) { echo 'err cannot read file'; // TODO curl internet } else { $res = Parser::parse($file); var_dump($res); }
based on P561P
Output for 5.3.21 - 5.4.45, 5.5.33, 5.6.18 - 5.6.19, 7.0.4, 7.0.20, 7.1.5 - 7.2.0
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: System error in /in/ikB4Y on line 54 Warning: file_get_contents(http://korben.info/feed): failed to open stream: php_network_getaddresses: getaddrinfo failed: System error in /in/ikB4Y on line 54 err cannot read file
Output for 5.5.24 - 5.5.32, 5.5.34 - 5.6.17, 5.6.20 - 7.0.3, 7.0.5 - 7.0.6, 7.1.0
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /in/ikB4Y on line 54 Warning: file_get_contents(http://korben.info/feed): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /in/ikB4Y on line 54 err cannot read file