Skip to main content

How to fetch product fom flipkart

<?php $content = file_get_contents("http://stackoverflow.com/questions/18049289/how-to-fetch-data-text-from-an-external-website-with-php-if-possible");

//print_r($content); ?
http://www.flipkart.com/samsung-galaxy-on5/p/itmedhx3uy3qsfks?pid=MOBECCA5FHQD43KA&al=OVGKKfDmwQTN6O85o2%2BD0MldugMWZuE7Qdj0IGOOVquS3ymugNFh%2BcWgDvEB%2BoXknGEqmG%2Fg55Q%3D&ref=L%3A4113169617862935247&srno=b_1

$url = "http://www.flipkart.com/samsung-galaxy-on5/p/itmedhx3uy3qsfks?pid=MOBECCA5FHQD43KA&al=OVGKKfDmwQTN6O85o2%2BD0MldugMWZuE7Qdj0IGOOVquS3ymugNFh%2BcWgDvEB%2BoXknGEqmG%2Fg55Q%3D&ref=L%3A4113169617862935247&srno=b_1&affid=amitlabnol";

$response = getPriceFromFlipkart($url);


//echo json_encode($response);
 ?>
 <div class="product">
     <a href=""
    <b><?php echo $response['title']; ?></b><br />
    <img src="<?php echo $response['image']; ?>" name="product_img" /><br />
    <b><?php echo $response['price']; ?> </b>

 </div>


 <?php
/* Returns the response in JSON format */

function getPriceFromFlipkart($url) {

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (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-src="([^"]*)"/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;
}
 

Comments

Post a Comment