Issues displaying returned xml file data in php file

I’m struggling to get a php page to echo data from an xml file.
I’m loading the XML file into the PHP file using simplexml_load_file.
I’ve attached a screenmshot of what the file looks like when viewed in a browser and the code below is the page:
(with any login data removed)

<?php
$xml=simplexml_load_file('https://website.co.uk/section/service.asmx/GetUsedValuation?Subscriber_ID=XXXXX&Password=XXXXX&Database=CARS&CAPID=67938&CAPCODE=&RegistrationDate=2015/06/14&DatasetDate=2015/06/14&JustCurrent=true&Mileage=12000');
header('Content-Type: text/xml');
echo $xml->asXML();
?>

What I need is to get the Retail, Clean, Below and Average values to echo onto the page. All I keep getting is errors no matter what way I try to do it.

I don’t have a lot of experience working with XML. Previously I’ve user cURL for something similar but the service for this one won’t work with the cURL parameters.

Looking for any help here to get this working please.

Can’t see a screenshot. What errors do you get?

That’s the screenshot for what the xml file looks like:

I keep getting errors anytime I try to get the individual node values from the file.
Such as:
XML Parsing Error: junk after document element

When I add in anything like the below after the origianl code:

foreach($xml->asXML() as $values) {
    echo $values->Retail;
    echo $values->Clean;
    echo $values->Average;
    echo $values->Below;
} 

Any ideas how I can get this to work?

Looks like the XML itself is invalid.

I can’t do anything about the format of the xml doc, it’s produced by a web service. Is there anyway I can get around this?
I can get the full xml file to echo on the page using asXML.

First you would need to inspect the XML source code if there is something suspicious (well, the XML parser says there is, so there should be something). It might be necessary to view the XML in a hex editor to find non-printable characters (e.g. BOM).

Which is the reason your foreach doesn’t work. you’re trying to iterate over a string. but that would produce a different message.

Grab it as a string, do a string search for the opening and closing tags that surround the values you want, and then split out the data between them? If the XML really is invalid, then you have to stop treating it as XML and just pretend it’s a string.

Excellent that sounds like a much better option. Any particular way that’s best to grab the xml as a string?

I can’t remember the function name - file_get_contents() perhaps?

Have you contacted the data supplier in case they have any sample code to suggest where you might be going wrong? If this is a paid-for data service, or at least a supported one, you’d imagine that others are using it successfully.

Does this page help at all? http://forums.devshed.com/php-development-5/parsing-xml-document-687879.html

Yeah I tried that and they basically have no php support!
Thanks for that, I’ve now got the following code:

$contents = file_get_contents($xml);
echo $contents;

Which echos true and then the four values I need on the screen.
How do I do the next part so that I can get the values into php variables?

Thanks for the help with this!

Fix the XML as described in post #7. Then read the valid XML string and work with that.

Do a search on “string search” and “string split”, that will give you the functions you need to work with the string.

But I meant use file_get_contents() instead of simple_xml_load_file(), not on the $xml variable you’ve already got. Get the whole thing as a string, search for "<Retail>", search for "</Retail>", then grab the characters between them, repeat for the other three values.

Much “nicer” though to figure out why the XML parsing isn’t working properly for you.

I am using the file_get_contents() using the code below,:

$contents = file_get_contents('https://website.co.uk/section/service.asmx/GetUsedValuation?Subscriber_ID=XXXXX&Password=XXXXX&Database=CARS&CAPID=67938&CAPCODE=&RegistrationDate=2015/06/14&DatasetDate=2015/06/14&JustCurrent=true&Mileage=12000');
echo $contents;

And when I go to the file in a browser now shows the following on screen:
true 12850 11250 10550 9925

I’ll look into what you’ve suggested and see if I can get it to do what I need.

Thanks again

That’s because the browser thinks–or rather, is told that–it’s an HTML file and applies the rendering for that.

I know that’s XML, but it looks like it’s a particular subset - SOAP
http://php.net/manual/en/book.soap.php

It’s been a while since I’ve worked with it. I find JSON easier to work with and it’s usually enough for what I need for my own stuffs. But I think you will have better luck working with it as it a SOAP result instead of XML or a string.

If it’s indeed SOAP, then there is no business working with the XML directly. Just use the SOAP processor of your choice.

1 Like

Thanks for the help with this guys, I’ll have a look at the different links you’ve posted and wee which is going to work with this.

Appreciate the help

I can’t see where the code you posted might display “true”, but the other numbers are the four values you are trying to extract, so surely you could just split them by the space character and use them? Until they send them in a different order, that is.

Near the beginning

<Success>true</Success> 

I guess it could be done that way. Maybe easy, but hacky and fragile. Unless this is going to be a one time only use then toss out thing I would advise against taking shortcuts that could come back to bite.

Of course, and there’s more to it because as someone mentioned earlier, the only reason it’s missing loads of other stuff out of the display is because of the browser. If the OP were to var_dump($contents) instead, it might clarify it.

1 Like