0

I'm trying to echo the XML content at this URL but I'm having difficulty. Here's what I have so far:

$url = "GetVideosServlet?queryId=1";

$xml = simplexml_load_file($url);

$value = (string) $xml->results->item[0]->id;

echo $value;

I keep getting the error that I'm trying to get the property of a non-object. But I was under the impression simplexml_load_file converts my XML string INTO an object??

If anyone could show me how to echo out any of the content, I'd be very grateful.

3
  • That means $xml didn't get turned into an object. If it's false, it didn't work. Commented Jul 2, 2012 at 11:13
  • but when I use print_r($xml), I SEE an object?? Commented Jul 2, 2012 at 11:14
  • What does it give? (As in, put that in the question.) Commented Jul 2, 2012 at 11:17

3 Answers 3

4

I think you just miss a tag which is query, try:

$value = (string) $xml->query->results->item[0]->id;
echo $value;
Sign up to request clarification or add additional context in comments.

Comments

2

When you are debugging print_r and var_dump are very handy! for example in this case if you dumped the $xml right after loading it you would have noticed that you missed out the SimpleXMLElement Object query.

$url = "http://176.34.224.80/REMPADRecSys/GetVideosServlet?queryId=1";
$xml = simplexml_load_file($url);
echo "<pre>";
print_r($xml);

Would give you the output:

SimpleXMLElement Object
(
    [query] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [id] => 1
                )

            [results] => SimpleXMLElement Object
                (
                    [item] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [id] => GZ7w39jpqwo
                                    [rank] => 1
                                    [explanation] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a massa lectus, sed convallis sapien. Curabitur sem mauris, ullamcorper ut. 
                                )

and so the correct reference would have been $xml->query->results->item[0]->id; like @Lake mentioned. Happy debugging.

2 Comments

Whoa...did you do that by echoing <pre> first? ...how does this work!?
yeh. It is really helpful. Check out w3 schools <pre> tag basically just preserves the line breaks and spaces.
0
simplexml_load_file(rawurlencode('http://176.34.224.80/REMPADRecSys/GetVideosServlet?queryId=1'));

try this.

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.