Trying To Get Property Of Non-object?
Solution 1:
You have two problems in your code:
The obvious problem is that $html->getElementsByTagName('article')->item(0)
is not an object. Specifically, it is null, since the HTML you're parsing doesn't actually contain any article
elements. You could've figured this out yourself by following Devon's advice and viewing the value of $html->getElementsByTagName('article')->item(0)
using var_dump()
.
Now, why doesn't your HTML contain any article
elements? Well, the real problem turns out to be that the loadHTML()
method will load HTML from a string and parse it. That is to say, when you call $html->loadHTML($url);
, PHP will parse the contents of the string variable $url
as HTML code, and give you a DOMDocument representing the result. Given that you named the variable $url
, I'm pretty sure that's not what you want.
What you actually want to use instead is probably loadHTMLFile()
, which actually loads HTML code from a named file (or, apparently, URL), rather than from a PHP string.
Post a Comment for "Trying To Get Property Of Non-object?"