For some reason I can pull out a DomElement object into a variable but try to access it’s methods and I get the above error. Use it in the foreach and the DomElement object works just fine.
function pb_get_image($output){
$doc = new DOMDocument();
@$doc->loadHTML($output);
$tags = $doc->getElementsByTagName('img');
/*
$tags = object(DOMNodeList)#244 (1) { ["length"]=> int(1) }
*/
$test = $doc->getElementsByTagName('img')->item(0);
/*
$test = object(DOMElement)#242 (17) { ["tagName"]=> string(3) "img" ["schemaTypeInfo"]=> .....
*/
// $width = $test->getAttribute('height');
/* This returns:
Fatal error: Call to a member function getAttribute() on a non-object in D:sharedwpdeva01wp-contentthemespicturebloghome.php on line 38 */
echo "
width = $width";
echo '
$tags =';
var_dump($tags);
echo '
$test =';
var_dump($test);
foreach ($tags as $tag) {
echo '
$tag =';
var_dump($tag);
/*
tag = object(DOMElement)#242 (17) { ["tagName"]=> string(3) "img" ["schemaTypeInfo"]=> ...
*/
$src = $tag->getAttribute('height'); //this works no problem!?!
var_dump($src);
}
return $output;
}
I ended up discovering that the issue was the fact that sometimes the $output would be false when it comes into the function. A simple if $output {process..} fixed the issue.
Before discovering that I also tried using the $xml = simplexml_load_string($output); method of parsing html. I would still get the same errors as above. Once I discovered that $output would sometimes be boolean (in fact the first time this function is called, it’s false), I stuck with the simpleexml_load_string function. I’ll post that next.