DOMxpath query returns nothing

房东的猫 提交于 2019-12-25 12:12:20

问题


I need to load, modify and output some 3rd party iframe on my webpage. As suggested, I had created the intermediary page that simply contains <iframe> with its src attribute.

The php code that outputs the page looks like this:

$iframe->loadHTML(file_get_contents("http://example.com/iframe_page.php")); //creating DOM object, see htm content below
$xpathObj= new DOMXPath($iframe);//creating DOMXPath object
foreach ($xpathObj->query('//div[@id="specific_id"]') as $node){ //this query returns nothing
    $node->parentNode->removeChild($node);//i need to remove the div with that id, but there is nothing to remove
}
echo $iframe->saveHTML($iframe->documentElement);//the iframe output works fine

And the content of my iframe looks something like this:

<html>
    <head>
    </head>
    <body>
        <div>
            <div>
                <div id="specific_id">
                </div>
            </div>
        </div>
    </body>
</html>

I've even tried disabling JS to see if this div is placed there by front-end code and nope with JS disabled the structure of the document looks exactly the same.


回答1:


The answer is rather simple. You CAN'T manipulate iframe element crossdomain using PHP DOMDocument.



来源:https://stackoverflow.com/questions/43718492/domxpath-query-returns-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!