tags:

views:

34

answers:

2

Ever seen that error? Yeah? Cause i've been sitting here for over 3 hours now trying to figure out why it's there. :(

The URL passed in is correct, and the iframe on the page does exist.

$doc = new DOMDocument();
@$doc->load($url);
$lst = $doc->getElementsByTagName('iframe');
$iframe = $lst->item(0);
$returnLinks[] = $iframe->attributes->getNamedItem('src')->value; // this line is causing the error!

Any ideas what's up? Here's the error once again:

Fatal error:  Call to a member function getNamedItem() on a non-object in ....

If it matters, that codeblock is placed within a loop.

A: 

[EDIT]: If all you want is src:

<?php
    $data = '<iframe src ="html_intro.asp" width="100%" height="300">
      <p>Your browser does not support iframes.</p>
    </iframe>';

    $doc = new DOMDocument();
    @$doc->loadHTML($data);
    $lst = $doc->getElementsByTagName('iframe');
    $iframe = $lst->item(0);

    echo $iframe->getAttribute ('src');

    //Output: html_intro.asp
?>

Old Answer:

attributes is a function not a property. Do it like this:

 $returnLinks[] = $iframe->attributes()->getNamedItem('src')->value;
shamittomar
Yeah, i got this:NULLobject(DOMNamedNodeMap)#1 (0) {}
Nike
Now, this is another problem. This question's problem of `Fatal Error` is gone :)
shamittomar
Great! :( Any ideas why it's returning NULL? I have a feeling it's not actually finding the iframe element in the html for some reason..
Nike
@Nike, I have updated the code with how to get `src` with complete input/output example. Hope this helps.
shamittomar
Nothing is really changed in the code, except instead of using load() to load the page directly, i'm storing the html in a var which i'm later referring to with loadHTML(), right?
Nike
WOOH! I don't really understand why it works, but it does now. Thanks!
Nike
@Nike, the code which has changed is ` $iframe->getAttribute('src')`. This thing do the magic.
shamittomar
+1  A: 

Apparently, $iframe->attributes is not evaluating to an object, try var_dump($iframe->attributes)

aularon
Nothing changed.
Nike
did the error message change? what the `var_dump($iframe->attributes)` output?
aularon
The error disappears and this is shown instead: NULLobject(DOMNamedNodeMap)#1 (0) {}
Nike