1

I am trying to find out how this would work

For testing purposes, I have made two websites.

One is calling a REST service from the other

I pull the xml data with file_get_contents

if I echo it, I can see a string off data.

But how can I use simpelxml on it, extract data from the nodes themselves?

If I use simplexml_load_file($url), I get some error saying xml declaration only allowed at the start off the document?

I have this in my testfile

<?php

$url='http://www.woonbel.nl/gps/setgpsloc';
//not working
$xml =simplexml_load_file($url);   

print_r($xml);

//just a string
$xml=file_get_contents($url);
echo "<h3>$xml</h3><br>";

?>

this was the xml I send.

I send this from a class file that I included in the top off my php file if I am sure the webservice is called, maybe that has someting to do with the declaration error?

header('Content-type: text/xml');
echo "<?xml version=\"1.0\"?>\n";
echo "<response>\n";
echo "\t<status>$status_code</status>\n";
echo "\t<fout>Geen</fout>\n";
echo "</response>";

Thanks, Richard

3
  • 2
    Without seeing the particular file, it's hard to tell what the error might be. Commented Jul 23, 2009 at 18:50
  • The xml looks ok. Check what the file_get_contents returns and check for anything before the start of <?xml Commented Jul 23, 2009 at 22:38
  • file_get_contents returns the data contained in the xml tags in a string. I can see, because I have echo'd it to the page. Commented Jul 24, 2009 at 2:47

4 Answers 4

3

Sounds like there is a blank line at the top of the file, when it should start with the xml declaration. For example:

<?xml version="1.0" encoding="utf-8"?>
Sign up to request clarification or add additional context in comments.

5 Comments

that is the part I don't understand If I want to pull xml data in, then the whole file has to be an xml file. So, then it is no longer an html file?
then, exactly how does it work when you want to display the data from the xml?
Richard, you have to check the string you are getting and make sure it is valid xml. Then you'll be able to use simpleXML.
how do you check if it is valid xml? is file_get_contents still an option to get the xml, because I have that from other tutorials. Maybe I have to put file_get_contents into simplexml_load_string?
Well formed HTML is usually XHTML which is in fact also XML.
0

Have you got empty lines before the declaration?

Comments

0

Your REST service should be returning XML contents, something like this:

<?xml version="1.0" encoding="utf-8"?>
<results>
  <result>
    <value awesome="true">LOLCATS</value>
  </result>
</results>

You'd then do something along these lines to consume that REST service on the other site:

$xml = simplexml_load_string(file_get_contents('http://example.com/rest.xml'));

foreach($xml->result as $result) {
  $value = $result->value;
  $awesome = $result->attributes()->awesome;
  // do something here with our values and attributes
}

The PHP docs for SimpleXML contain more complicated/real-world examples.

For your specific XML:

$xml = simplexml_load_string(file_get_contents('http://example.com/rest.xml'));

$status = $xml->status;
$fout = $xml->fout;

4 Comments

yes, I just thought off that, only my enclosing tags are named response and the url does not contain any xml extension, just nouns.
Added in code for your specific XML. SimpleXML does a good job of making a usable PHP data structure out of an XML file.
thanks for editing my question. I am still missing something because I get that declaration error? I have exactly two lines in my testfile, namely these ones $url=the_url; $xml = simplexml_load_string(file_get_contents($url));
the solution was, that I needed a space between xml version 1.0 and encoding... That little thing was causing it.
0

The error message clearly states that there is a blank, line or character, at the beginning of the xml-data. That could be file-encoding -issue.

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.