Newegg Price Parser

Current Version: 1.0.0
Price: FREE

Newegg is one of the most popular retailers of computer components and related electronics on the internet. As a result many people use Newegg as their supplier and often as a pricing reference. A common problem is that people wish to utilize real-time pricing information from Newegg for various reasons — be it a small-scale computer business (where a wholesaler relationship is not possible due to limited sales volume), to include in a posting about computer components, or any other application you can imagine. Newegg does not provide an API or simple method to retrieve pricing information for general public consumption making a third party solution is necessary. This simple function will parse pricing and availability directly from a Newegg items page. Newegg’s mobile site is utilized to keep the amount of data transferred small and execution times to a minimum.

Dependencies
PHP 4.3+ with the ability to open remote url’s as files


The Function
This function uses Newegg’s mobile site to retrieve an items status or price. If the items price is available the function returns the item price. In the event that the price is unavailable and a known product status is available the products status is returned — otherwise the script simply returns “ERROR”. Possible returns include: The Item’s Price, SOLDOUT, DEACTIVATED, UNAVAILABLE, NOTFOUND, and ERROR. The definitions of these returns are self explanatory. A simple case statement would be sufficient to parse the returns into something more suitable for end-user consumption.

function neweggPrice($itemNumber){

        //URL's for newegg pricing site and to view item on site
        $neweggFullURL="http://www.newegg.com/Product/Product.aspx?Item=";
        $neweggURL="http://m.newegg.com/Product/Product.aspx?Item=";

        //readsize for fopen
        $readsize=4096;

        //open file and aquire shared lock
        $fv=fopen($neweggURL.$itemNumber, 'r');

        //initialize vars and get array of file
        $temp="";
        while ($line=fgets($fv, $readsize)) {
                $temp.= $line;
        }

        //relinquish lock and close file
        fclose($fv);

        //parse for price, make sure its not deactivated, make sure its not sold out, make sure its actually an item.
        if(strpos($temp, '<input type="submit"  class="button autoNotify" value="Auto Notify" />')){
                return array("SOLDOUT", $neweggFullURL.$itemNumber);
        }else if(strpos($temp, '<strong>Your Price:')){
                $temp=substr($temp, (strpos($temp, '<strong>Your Price:')+21), (strpos($temp, '</strong>&nbsp;</dd>')-strpos($temp, '<strong>Your Price:')-21) );
                $temp=str_replace(",", "", $temp);
                return array(floatval($temp), $neweggFullURL.$itemNumber);
        }else if(strpos($temp, '<dl><dd>Deactivated Item.</dd></dl>')){ 
                return array("DEACTIVATED", $neweggFullURL.$itemNumber);   
        }else if(strpos($temp, '<dl><dd>Currently Unavailable</dd></dl>')){
                return array("UNAVAILABLE", $neweggFullURL.$itemNumber);
        }else if(strpos($temp, 't find this Item. Please check your Item#.')){
                return array("NOTFOUND", $neweggFullURL.$itemNumber);
        }else{
                return array("ERROR", $neweggFullURL.$itemNumber);
        }
        
        //if we reached this point something went wrong. Return an error.
        return -1;
}



Example Usage
The above function must be included on the page you wish to retrieve an items price/status on. It can be included through a master functions file like many custom sites and cms’ utilize or simply inserted locally on the page it’s being utilized on. In addition to returning the items price the above function will also return the products status if pricing is unavailable. This example does not parse for returned status. It simply displays the information returned by the function.

// The function retuns an array
// [0] == price/status
// [1] == URL to view product on Newegg Site 
$itemPrice=neweggPrice("N82E16814129164");
echo "Item Number: <a href='".$itemPrice[1]."' target='_blank'>N82E16813130160</a> - ".$itemPrice[0]." <br/>";

Leave a Reply

2 Responses to Newegg Price Parser Function

  1. Dude,

    You rock. I’m going to build a table to load item numbers and basic description to make it easier to script an update page. You just made my day :)