1

I want to parse an external php feed. The address: http://www.hittadjur.se/feed.php?count=1

The output:

<?xml version="1.0"?>
<annons>
<rubrik>Wilja</rubrik>
<datum>2013-03-22</datum>
<ras>Chihuahua långhår</ras>
<ort>Göteborg</ort><bildurl>http://www.hittadjur.se/images/uploaded/thumbs/1363984467.jpg</bildurl><addurl>http://www.hittadjur.se/index.php?page=case&type=&county=32&subpage=show&case=1363984558</addurl>
</annons>

My PHP code that doesn't work:

$content = utf8_encode(file_get_contents('http://www.hittadjur.se/feed.php?count=1'));
$xml = simplexml_load_file($content);
echo $xml->annons->rubrik;

The reason I use the utf8_encode is that I receive this message if I don't:

parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xE5 0x6E 0x67 0x68

The error now is:

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity Any ideas? Thanks!

3 Answers 3

0

Try to pass full directory path if you are trying to load xmls held at your server

simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/example.xml')

or if you want to access xml by http protocol you will need to set allow_url_fopen ON in php.ini or

ini_set('allow_url_fopen ','ON');

in your code. or you can also do this if you are using php version <5

$temp = file_get_contents($url);
 $XmlObj = simplexml_load_string($temp); 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi! This is an external feed that's not located on my server. On which server should I set allow_url_fopen?
0

like alvaro vicario wrote, the problem are the parameter separators ( & ) in your urls. in xml, an ampersand is a entity marker ( = start of a named symbol (sequence) or numerical representation of a character code point ) and must be escaped.

either replace & by &amp; in your urls or mark the urls as literal text (CDATA section in xml speak): <![CDATA[http://...]]>.

eg. : <addurl><![CDATA[http://www.hittadjur.se/index.php?page=case&type=&county=32&subpage=show&case=1363984558]]></addurl>

if you are uncomfortable with the express utf8 conversion in your code and you know the character encoding of your data source, you may enhance the xml prologue (iso-8859-1 contains the offending å/0xE5of your xml):

<?xml version="1.0" encoding="iso-8859-1"?>

Comments

0

I'm afraid that the feed provides malformed XML. Apart from the encoding mess:

<addurl>http://www.hittadjur.se/index.php?page=case&type=&county=32&subpage=show&case=1363984558</addurl>
                                                   ^
                                                   \_ Data not properly escaped

I may be wrong but I don't think you can parse it using regular XML functions because they're designed for valid XML (that's the whole purpose of using XML in the first place).

Perhaps you can try with DOMDocument. It's designed for HTML so it can deal with invalid input but it can also do XML.

Edit: Here's a trick to fix invalid XML but, honestly, I'm not sure it's worth the effort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.