Reputation: 51
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
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"> <\/span>(.*?)<\/span>/', get_page($url),$matches1))
{
$data[$key]['price']=$matches1[1];
}
}
}
var_dump($data);
?>
Upvotes: 2
Reputation: 495
Try this for flipkart price:
preg_match('/<span class="selling-price.*?data-eVar48="(.*?)">/',$sourcestring,$matches);
echo $matches[1];
Upvotes: 3