File_get_contents () removes XML tags

In the browser, the following URL http://kulturarvsdata.se/raa/fmi/xml/10028201230001 is displayed as a regular XML file. but when i use

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

      

it removes all XML tags and just returns the contained text. Why is this happening and how to 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" 
}

      

+3


source to share


3 answers


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 the answer is: works great.

Perhaps you can look at a browser response that removes tags? This will at least be a common mistake some users ask about on Stackoverflow.

+3


source


Please, try

simplexml_load_file - Interprets XML file into object



<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.

if (file_exists('test.xml')) {
    $xml = simplexml_load_file('test.xml');

    print_r($xml);
} else {
    exit('Failed to open test.xml.');
}
?>

      

0


source


Had the same problem and I figured it changes the way I get information, so instead of using file_get_contents I used curl:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
curl_setopt ($ch, CURLOPT_TIMEOUT, 10);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec ($ch);
curl_close ($ch);

      

0


source







All Articles