Search code examples
phpiframesimple-html-dom

SimpleHTMLDOM - Get iframe src link


How to get the "thesrc" link from the iframe tag with SimpleHTMLDOM?

The source of the page i want the data from:

<div class="ui-tab-pane" data-role="panel" id="feedback">
 <iframe scrolling="no" frameborder="0" marginwidth="0" marginheight="0" 
width="100%" height="200" 
thesrc="//feedback.aliexpress.com/display/productEvaluation.html?productId=32795263337&ownerMemberId=228319068&companyId=237873399&memberType=seller&startValidDate=&i18n=true"></iframe>
</div>

So i tried:

<?php
include_once('simple_html_dom.php');

$html = file_get_html('https://www.aliexpress.com/item/Global-Version-Xiaomi-Redmi-Note-4-Mobile-Phone-3GB-RAM-32GB-ROM-Snapdragon-625-Octa-Core/32795263337.html');
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
foreach ($xpath->query("//div[#class='ui-tab-pane']") as $node){
echo $node->getElementsByTagName('iframe')[0]->getAttribute("thesrc");
echo $node->getElementsByTagName('div')[0]->nodeValue;
}

?>

It returns nothing. What i'm doing wrong?


Solution

  • You don't really need the XPath stuff to achieve this. Have a look here:

    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $iFrame = $dom->getElementsByTagName('iframe')->item(0);
    $src = $iFrame->getAttribute('thesrc');
    
    echo $src;
    

    Which gives you:

    //feedback.aliexpress.com/display/productEvaluation.html?productId=32795263337&ownerMemberId=228319068&companyId=237873399&memberType=seller&startValidDate=&i18n=true
    

    See it working here https://3v4l.org/41CRj