How to print an XML file to the screen in PHP?
This is not working:
$curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, '//rss.news.yahoo.com/rss/topstories'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($curl); curl_close ($curl); $xml = simplexml_load_string($result); echo $xml;Is there a simple solution? Maybe without SimpleXML?
Syscall
18.3k10 gold badges33 silver badges49 bronze badges
asked Jul 29, 2009 at 11:33
1
You can use HTTP URLs as if they were local files, thanks to PHP's wrappers
You can get the contents from an URL via file_get_contents() and then echo it, or even read it directly using readfile()
$file = file_get_contents('//example.com/rss'); echo $file;or
readfile('//example.com/rss');Don't forget to set the correct MIME type before outputing anything, though.
header('Content-type: text/xml');answered Jul 29, 2009 at 11:40
Josh DavisJosh Davis
27.9k5 gold badges51 silver badges67 bronze badges
1
Here's what worked for me:
<pre class="prettyprint linenums"> <code class="language-xml"><?php echo htmlspecialchars(file_get_contents("example.xml"), ENT_QUOTES); ?></code> </pre>Using htmlspecialchars will prevent tags from being displayed as html and won't break anything. Note that I'm using Prettyprint to highlight the code ;)
answered Aug 7, 2012 at 13:46
GabLeRouxGabLeRoux
15.4k13 gold badges61 silver badges78 bronze badges
0
You can use the asXML method
echo $xml->asXML();You can also give it a filename
$xml->asXML('filename.xml');answered Jul 29, 2009 at 11:57
Cristian TomaCristian Toma
5,6332 gold badges35 silver badges42 bronze badges
1
This worked for me:
echo(header('content-type: text/xml'));
Kevin Panko
8,14919 gold badges51 silver badges60 bronze badges
answered Apr 22, 2015 at 19:07
jimjim
2124 silver badges10 bronze badges
1
If you just want to print the raw XML you don't need Simple XML. I added some error handling and a simple example of how you might want to use SimpleXML.
<?php $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, '//rss.news.yahoo.com/rss/topstories'); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec ($curl); if ($result === false) { die('Error fetching data: ' . curl_error($curl)); } curl_close ($curl); //we can at this point echo the XML if you want //echo $result; //parse xml string into SimpleXML objects $xml = simplexml_load_string($result); if ($xml === false) { die('Error parsing XML'); } //now we can loop through the xml structure foreach ($xml->channel->item as $item) { print $item->title; }answered Jul 29, 2009 at 12:14
Tom HaighTom Haigh
56.6k20 gold badges112 silver badges140 bronze badges
Am I oversimplifying this?
$location = "//rss.news.yahoo.com/rss/topstories"; print file_get_contents($location);Some places (like digg.com) won't allow you to access their site without having a user-agent, in which case you would need to set that with ini_set() prior to running the file_get_contents().
answered Jul 29, 2009 at 11:39
SampsonSampson
261k74 gold badges530 silver badges559 bronze badges
0
To display the html/xml "as is" (i.e. all entities and elements), simply escape the characters <, &, and enclose the result with <pre>:
$XML = '<?xml version="1.0" encoding="UTF-8"?> <root> <foo>ó</foo> <bar>ó</bar> </root>'; $XML = str_replace('&', '&', $XML); $XML = str_replace('<', '<', $XML); echo '<pre>' . $XML . '</pre>';Prints:
<?xml version="1.0" encoding="UTF-8"?> <root> <foo>ó</foo> <bar>ó</bar> </root>Tested on Chrome 45
answered Sep 16, 2015 at 14:41
miklmikl
1,0071 gold badge18 silver badges31 bronze badges
1
If anyone is targeting yahoo rss feed may benefit from this snippet
<?php $rssUrl="//news.yahoo.com/rss/topstories"; //==================================================== $xml=simplexml_load_file($rssUrl) or die("Error: Cannot create object"); //==================================================== $featureRss = array_slice(json_decode(json_encode((array) $xml ), true ), 0 ); /*Just to see what is in it use this function PrettyPrintArray() instead of var_dump($featureRss);*/ function PrettyPrintArray($rssData, $level) { foreach($rssData as $key => $Items) { for($i = 0; $i < $level; $i++) echo(" "); /*if content more than one*/ if(!is_array($Items)){ //$Items=htmlentities($Items); $Items=htmlspecialchars($Items); echo("Item " .$key . " => " . $Items . "<br/><br/>"); } else { echo($key . " => <br/><br/>"); PrettyPrintArray($Items, $level+1); } } } PrettyPrintArray($featureRss, 0); ?>You may want to run it in your browser first to see what is there and before looping and style it up pretty simple
To grab the first item description
<?php echo($featureRss['channel']['item'][0]['description']); ?>You can see a demo here
answered Apr 18, 2016 at 11:08
ShapCyberShapCyber
3,1302 gold badges20 silver badges25 bronze badges
This works:
<?php $XML = "<?xml version='1.0' encoding='UTF-8'?> <!-- Your XML --> "; header('Content-Type: application/xml; charset=utf-8'); echo ($XML); ?>answered Aug 13, 2018 at 8:26
NullNull
8369 silver badges12 bronze badges
The best solution is to add to your apache .htaccess file the following line after RewriteEngine On
RewriteRule ^sitemap\.xml$ sitemap.php [L]and then simply having a file sitemap.php in your root folder that would be normally accessible via //www.yoursite.com/sitemap.xml, the default URL where all search engines will firstly search.
The file sitemap.php shall start with
<?php //Saturday, 11 January 2020 @kevin header('Content-type: text/xml'); echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <urlset xmlns="//www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="//www.sitemaps.org/schemas/sitemap/0.9 //www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> <url> <loc>//www.yoursite.com/</loc> <lastmod>2020-01-08T13:06:14+00:00</lastmod> <priority>1.00</priority> </url> </urlset>it works :)
answered Jan 11, 2020 at 7:20