Abbe Abbe - 9 days ago 6
PHP Question

file_get_contents() removes XML tags

In the browser the following url http://kulturarvsdata.se/raa/fmi/xml/10028201230001 shows up as a regular XML file. but when I'm using

file_get_contents('http://kulturarvsdata.se/raa/fmi/xml/10028201230001');


it removes all XML tags and just returns the containing text. Why is this happening and how do I avoid it?

Some response headers:

array(5) {
[0]=> string(15) "HTTP/1.1 200 OK"
[1]=> string(35) "Date: Thu, 01 Jan 2015 20:07:04 GMT"
[2]=> string(25) "Server: Apache-Coyote/1.1"
[3]=> string(43) "Content-Type: application/xml;charset=UTF-8"
[4]=> string(17) "Connection: close"
}

Answer

Unable to reproduce:

<?php
/**
 * http://stackoverflow.com/questions/27733997/file-get-contents-removes-xml-tags
 */

header('Content-Type: text/plain; charset=utf-8');
echo substr(file_get_contents('http://kulturarvsdata.se/raa/fmi/xml/10028201230001'), 0, 256);

<?xml version="1.0" encoding="UTF-8"?><pres:item xmlns:pres="http://kulturarvsdata.se/presentation#"><pres:id>10028201230001</pres:id><pres:entityUri>http://kulturarvsdata.se/raa/fmi/10028201230001</pres:entityUri><pres:type>Kulturlämning</pres:type><pres

So answer is: Works fine.

You perhaps look at the response in the browser which removes the tags? This at least would be a common mistake some users on Stackoverflow ask about.

Comments