2

I am trying to fetch product specification and feature list from flipkart. I am using the below code but I am not able to fetch specification.

Is there anything that can make my code complete.

<?php
$url = 'http://www.flipkart.com/samsung-415-l-frost-free-double-door-refrigerator/p/itmedp6zcppxvhgh?pid=RFREDP6ZJXFY5QMK&al=Xh6p4IpEIjAx6PWgfu6yt8ldugMWZuE7%2BW7da8XnwKRuC2TkVUlPYWLhfoM4PDZcEqn50nOHN48%3D&ref=L%3A1683055601844045008&srno=p_2&query=samsung+rt42&otracker=from-search';
$response = getPriceFromFlipkart($url);
echo json_encode($response);
/* Returns the response in JSON format */
function getPriceFromFlipkart($url) {

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, "Chrome/49.0.2623.110 (Windows; U; Windows NT 10.10; labnol;) ctrlq.org");
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
curl_close($curl);    

$regex = '/<meta itemprop="price" content="([^"]*)"/';
preg_match($regex, $html, $price);

$regex = '/<h1[^>]*>([^<]*)<\/h1>/';
preg_match($regex, $html, $title);    

$regex = '/data-zoomimage="([^"]*)"/i';
preg_match($regex, $html, $image);

if ($price && $title && $image) {
    $response = array("price" => "Rs. $price[1].00", "image" => $image[1], "title" => $title[1], "status" => "200");
    
} else {
    $response = array("status" => "404", "error" => "We could not find the product details on Flipkart $url");
}
 
 return $response;
}
?>
SSK
  • 3,444
  • 6
  • 32
  • 59
anju jain
  • 21
  • 2

1 Answers1

0

There is no magical regexp that will do that. You'll probably want to mix several regexes and code to get the specifications.

A starting point could be using <div[^>]*>\s*Specifications\s*<\/div>(.*?)<div[^>]*>\s*Questions\s*and\s*Answers

See: https://regex101.com/r/Tanr0H/4

This will get the html response from "Specifications" up to "Questions and Answers"

Please, see lazy vs greedy to understand how (.*?) works: What do 'lazy' and 'greedy' mean in the context of regular expressions?

Also it may be a good idea to use some library to parse html

How do you parse and process HTML/XML in PHP?

Julio
  • 5,208
  • 1
  • 13
  • 42