0

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
    If you have the official legal permission to fetch the price from their website, you should contact them to obtain a price feed in xml or csv format. And yes it affects the page load. You should cache the prices (memcache, redis, sql). – Daniel W. Sep 02 '14 at 11:49
  • can u just remove dot and try once DOTDOnPopUpToaster_Sep2.&icmpid=hp_dotd_3_DOTDOnPopUpToaster_Sep2.? – Punitha Subramani Sep 02 '14 at 11:51
  • Thinking to store prices in MySQL to reduce time from fetching again form server – user3811305 Sep 02 '14 at 11:51
  • Tried to remove DOTDOnPopUpToaster_Sep2.&icmpid=hp_dotd_3_DOTDOnPopUpToaster_Sep2.? still getting same errors – user3811305 Sep 02 '14 at 11:53
  • You just need to disable the warnings, as invalid html is common. See this answer http://stackoverflow.com/questions/11819603/dom-loadhtml-doesnt-work-properly-on-a-server – Steve Sep 02 '14 at 11:56
  • Will then it will provide output? – user3811305 Sep 02 '14 at 11:57
  • Tried removing warning but gettings last two errors Notice: Undefined property: DOMDocument::$document in E:\Local server\htdocs\store\scripts\getprice.php on line 7 Fatal error: Call to undefined function getElementsByTagName() in E:\Local server\htdocs\store\scripts\getprice.php on line 7 – user3811305 Sep 02 '14 at 12:01

1 Answers1

0
$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.

MontrealDevOne
  • 1,034
  • 3
  • 17
  • 30
  • Thnx that helped but now i am just receiving warnings but no output – user3811305 Sep 02 '14 at 12:16
  • What are the warnings? – MontrealDevOne Sep 02 '14 at 12:29
  • Warning: DOMDocument::loadHTML(): Unexpected end tag : span in Entity, line: 261 in E:\Local server\htdocs\store\scripts\getprice.php on line 6 Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity, line: 293 in E:\Local server\htdocs\store\scripts\getprice.php on line 6 Warning: DOMDocument::loadHTML(): htmlParseEntityRef: no name in Entity, line: 293 in E:\Local server\htdocs\store\scripts\getprice.php on line 6 .... Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 6125 in E:\Local server\htdocs\store\scripts\getprice.php on line 6 – user3811305 Sep 02 '14 at 12:37
  • @user3811305 turn off the warnings as i suggested earlier – Steve Sep 02 '14 at 14:34
  • I mean my script doesn't return anything. – user3811305 Sep 02 '14 at 14:52
  • @user3811305 do you know any PHP? – MontrealDevOne Sep 02 '14 at 14:58