views:

439

answers:

3

Hello,

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

A: 

Have you got empty lines before the declaration?

koen
what declaration do I need?
Richard
+3  A: 

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"?>
Peter Di Cecco
that is the part I don't understandIf 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?
Richard
It is not an HTML file, correct.
ceejayoz
then, exactly how does it work when you want to display the data from the xml?
Richard
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.
koen
Alternative:http://www.php.net/manual/en/domdocument.loadhtml.php
koen
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?
Richard
Well formed HTML is usually XHTML which is in fact also XML.
Peter Di Cecco
A: 

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;
ceejayoz
yes, I just thought off that, only my enclosing tags are named response and the url does not contain any xml extension, just nouns.
Richard
I have to see if that will work.
Richard
They'll work fine, this is just example code. Tweak at will.
ceejayoz
Added in code for your specific XML. SimpleXML does a good job of making a usable PHP data structure out of an XML file.
ceejayoz
thanks for editing my question. I am still missing somethingbecause 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));
Richard
the solution was, that I needed a space between xml version 1.0 and encoding... That little thing was causing it.
Richard