user3164590
user3164590

Reputation: 51

Grab Data using php curl from different domain urls

I need to fetch prices from different domain urls using php preg match here I am facing problem prices fetching but showing wrong price. Here i need to fetch flipkart,amazon prices from urls. get_page function is curl to fetch data.

code

<?php
$urls=array('http://www.flipkart.com/moto-e/p/itmdvuwsybgnbtha?pid=MOBDVHC6XKKPZ3GZ','http://www.amazon.in/gp/product/B00Q2HGF3Q');

foreach($urls as $url)
{
//flipkart
if(preg_match("/flipkart\.[com|in]/is",$url))
{
    $data['type'] = "flipkart.in";

   if(preg_match("/class=\"price[\s]fk-display-block\">Rs\.(.*?)<\/span>/is", get_page($url), $m))
    {
        $m[1] = strtolower($m[1]);
        $data['price'] = (float) str_replace(array('rs.', ','),'',$m[1]);

    }
    elseif(preg_match("/data\-evar48=\"([0-9+]+?)\"/is", get_page($url), $m))
    {
    $m[1] = strtolower($m[1]);
    $data['price'] = (float) str_replace(array('rs.', ','),'',$m[1]);
    }

}
//amazon
    if(preg_match("/amazon\.in/is",$url))
    {
    $data['type'] = "amazon.in";
    if(preg_match("/class=\"currencyINR\"[\s]style=\"display:none\">Rs.[\s]<\/span>(.*?)<\/span>/is", get_page($url), $m))
    {
    $m[1] = strtolower($m[1]);
    $data['price'] = (float) str_replace(array('rs.', ','),'',$m[1]);
    }
    }

var_dump($data);

}



?>

Upvotes: 0

Views: 432

Answers (2)

Amit Kumar Sahu
Amit Kumar Sahu

Reputation: 495

Try this new code this will gives flipkart and amazon price seperatly:

<?php
$urls=array('http://www.flipkart.com/moto-e/p/itmdvuwsybgnbtha?pid=MOBDVHC6XKKPZ3GZ','http://www.amazon.in/gp/product/B00Q2HGF3Q');

foreach($urls as $key => $url)
{
//flipkart

if(preg_match("/flipkart\.[com|in]/is",$url))
{
    $data[$key]['type'] = "flipkart.in";

   if(preg_match('/<span class="selling-price.*?data-eVar48="(.*?)">/',get_page($url),$matches))
    { 
        $data[$key]['price']=$matches[1];
    }

}
//amazon
    if(preg_match("/amazon\.in/is",$url))
    {
        $data[$key]['type'] = "amazon.in";
        if(preg_match('/<span class="currencyINR">&nbsp;&nbsp;<\/span>(.*?)<\/span>/', get_page($url),$matches1))
        {
            $data[$key]['price']=$matches1[1];
        }
    }



}

var_dump($data);
?>

Upvotes: 2

Amit Kumar Sahu
Amit Kumar Sahu

Reputation: 495

Try this for flipkart price:

preg_match('/<span class="selling-price.*?data-eVar48="(.*?)">/',$sourcestring,$matches);
echo $matches[1];

Upvotes: 3

Related Questions