Unable to fetch Price

254 views Asked by At

I am trying to fetch price from a online store.

Here i am using this code..

<?php
function getPrice($site){
    $html = file_get_contents($site);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $contents = $dom->document.getElementsByTagName("span");        

    $price = "";
    for($i = 0; $i < $contents->length; $i++){
        $content= $contents->item($i);
        if($content->getAttribute('class') == "fk-font-verybig pprice vmiddle fk-bold"){
            $price = $content->getAttribute('value');
        }
    }
    echo $price;
}

$website = "http://www.flipkart.com/sogo-ss-5365-750-w-pop-up-toaster/p/itmdz3hgfjzgfp4v?pid=PUTDYWT2UHPCDCG8&offer=DOTDOnPopUpToaster_Sep2.&icmpid=hp_dotd_3_DOTDOnPopUpToaster_Sep2.";

getPrice($website);


?>

my script return error

Warning: DOMDocument::loadHTML(): Unexpected end tag : span in Entity, line: 261 in E:\Local server\htdocs\store\scripts\getprice.php on line 5

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity, line: 293 in E:\Local server\htdocs\store\scripts\getprice.php on line 5

...................................................................

Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 6160 in E:\Local server\htdocs\store\scripts\getprice.php on line 5

Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 6

Fatal error: Call to undefined function getElementsByTagName() in E:\Local server\htdocs\store\scripts\getprice.php on line 6

Is it ok to fetch price like this because the store keep changing price of its product. Is there any other alternative way to do so?

Will this script affect my server performance because ever time user visit a product page on my website it will fetch prices from 5 different stores to comapare prices.

1

There are 1 answers

6
MontrealDevOne On
$contents = $dom->document.getElementsByTagName("span");

Your $dom->document is failing because DOMDocument class does not have a property named 'document'.

Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 6

So this might work

$contents = $dom->getElementsByTagName("span");

The above should work.

I recommend iterating over $contents instead of echo.

Even print_r would help you see the structure of the nodes in $contents.