Search code examples
phphtmlfunctionxpathsimple-html-dom

PHP not getting right link from path


Ok, so I have literally never been so confused. As you can see I have pretty much the same function twice here (I know that may seem stupid but it is just easier for me to read for my page when it's like this - but that isn't the point of this)

The first one goes to the link it's given (http://www.blade-edge.com/images/KSA/Flights/craft.asp?db=dunai) then follows the path to get the img src of https://i.sstatic.net/pBtYR.png

But the second function doesn't get the src of the image it's pointing to (which would be https://i.sstatic.net/VXgGm.png) but instead gets the src for a completely different image on the page http://www.blade-edge.com/images/KSA/Flights/prev.png.

I am sure this is a really stupid mistake that I have just overlooked but I can't work it out.

Anyone?

    function getImage(){
        $page = file_get_html(getPageURL());
        $element = $page->find("html/body/div/div[1]/center/table/tbody/tr[1]/td/table/tbody/tr/td[1]/div/div/img");
        $imgLink = $element[0]->getAttribute("src");
        echo "<img id='shipImage' src='".$imgLink."'></img>";
    }

    function getMap(){
        $page = file_get_html(getPageURL());
        $element = $page->find("/html/body/div/div[1]/center/table/tbody/tr[2]/td/center/img");
        $imgLink = $element[0]->getAttribute("src");
        echo "<img id='shipMap' src='".$imgLink."'></img>";
    }

Solution

  • The following works for me:

    function getImage($imageType){
        $page = file_get_html(getPageURL());
        $element = $page->find("/html/body/div/div[1]/center/table/tbody", 0)->children($imageType)->find("img");
        $imgLink = $element[0]->getAttribute("src");
        return $imgLink;
    }
    
    echo "<img id='shipImage' src='" . getImage(0) . "'></img>"; // Spacecraft image
    echo "<img id='shipMap' src='" . getImage(1) . "'></img>"; // Map image
    

    I won't try to guess the reason behind the problem, as I do not know the innards of the library.