1
$url = "http://www.amazon.in/gp/product/B00UMSAWOY/";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_exec($ch);
curl_close($ch);

preg_match_all("/<span>(.*)<\/span>/", $result, $matches);
print_r($matches);
foreach($matches[1] as $val)
{
    echo $val."<br>";
}

If I run this query I am getting the result as 996-2015, Amazon.com, Inc. or its affiliates. But I need all the span values that are available in the link. The thing I want to print is product name and its price. Can anyone help solving this problem?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Karthickeyan
  • 479
  • 7
  • 22
  • 1
    [Don't parse HTML with regex!](http://stackoverflow.com/a/1732454/418066) – Biffen Jul 01 '15 at 11:48
  • $dom = new DOMDocument(); $dom->loadHTML($html); $selector = new DOMXPath($dom); $results = $selector->query('//span'); foreach($results as $node) { echo $node->nodeValue . PHP_EOL; }sorry @viral your answer is fine and its working. can you help me in getting the product name and its price – Karthickeyan Jul 01 '15 at 12:37

2 Answers2

2
 <?php
 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.amazon.in/gp/product/B00UMSAWOY/');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($ch);
curl_close($ch);    

 $dom = new DOMDocument;
 @$dom->loadHTML($html);
 $xpath = new DOMXPath($dom);
$searchNode = $dom->getElementsByTagName( "span" ); 
 foreach ($dom->getElementsByTagName('span') as $tag)                  
{
//echo $tag->getAttribute('id');
if($tag->getAttribute('id')=='productTitle')
{
    echo $tag1=$tag->nodeValue . '<br/>';
}
if($tag->getAttribute('id')=='priceblock_saleprice')
{
    echo $tag1=$tag->nodeValue . '<br/>';
}
}
?>

i have found out the solution friends. thanks for helping

Karthickeyan
  • 479
  • 7
  • 22
0

Below code will solve your problem

preg_match_all('/<span>.*?<\/span>/is', $result, $matches);
viral
  • 3,724
  • 1
  • 18
  • 32
anilviradiya
  • 139
  • 7