0

I have a RSS feed URL and I am using following PHP function to extract values from it.

$xml = simplexml_load_file($url);

Its working perfect when the $url generates some results. And I am using count() function to count the length of array thats extracted from RSS feeds.

$length=sizeof($xml->rs[0]->r);

But when there is no result in RSS it gives me error

Trying to get property of non-object in /home/****/public_html/index.php on line 5 

So is there a way to echo a message if there are no results in RSS feed URL.

And when i do print_r($xml) ob no results $xml , i get

SimpleXMLElement Object ( [@attributes] => Array ( [type] => noresults [code] => 1.1 ) [title] => SimpleXMLElement Object ( ) [subtitle] => SimpleXMLElement Object ( ) [text] => SimpleXMLElement Object ( ) [base] => SimpleXMLElement Object ( ) ) )

I searched everywhere and found no solution.. Thanks for your help.

2
  • I think you've striped out part of the error message. Can you please edit the question and fix it? Commented Jul 26, 2014 at 9:17
  • Notice: Trying to get property of non-object in /home/****/public_html/index.php on line 5 Commented Jul 26, 2014 at 9:25

2 Answers 2

1

You can do this:

if(is_a($xml,"SimpleXMLElement"))
{
    //process XML here
}
else
{
    die("Unable to load XML from URL");
}

The is_a function takes two arguments - the first is an object variable and the second is the class to which we are checking if it belongs to.

Based on the update to your question it looks like you are getting an XML object returned by the service irrespective of whether there are results or not. In that case you need to examine the returned XML in order to check if it has value or is saying that there are no results.

Pay attention to the return value ( [@attributes] => Array ( [type] => noresults [code] => 1.1 )

You can use SimpleXMLElement::attributes function to loop and example attributes. Everything you need to test it out is documented pretty well in the PHP Manual for SimpleXMLElement

Sign up to request clarification or add additional context in comments.

2 Comments

Same error and when i try to print_r($xml) which fetches no result , gives something like this SimpleXMLElement Object ( [@attributes] => Array ( [type] => noresults [code] => 1.1 ) [title] => SimpleXMLElement Object ( ) [subtitle] => SimpleXMLElement Object ( ) [text] => SimpleXMLElement Object ( ) [base] => SimpleXMLElement Object ( ) ) )
But that is correct. The XML url you are hitting is giving you an XML object which states that it was not able to find any results. See this line - [@attributes] => Array ( [type] => noresults [code] => 1.1 ) What you need to do is parse the XML result and check its contents to see if an XML with result or without result is returned.
0
$xml = simplexml_load_file($url);

if ($xml && property_exists($xml, 'rs') && is_array($xml->rs) && isset($xml->rs[0]) && is_object($xml->rs[0]) && property_exists($xml->rs[0], 'r')) {
    // do something with $xml->rs[0]->r
} else {
    echo "Not available to access $xml->rs[0]->r";
}

1 Comment

I just want to tell you how to check the variable/property/array is set or not. To simplify it, just use: if (@isset($xml->rs[0]->r)) to check if you don't want to edit php.ini to suppress the notices when you are accessing an undefined variable.

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.