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> </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/>";










Feb 4th, 2012 at 11:24 am
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 :)
[Reply]
Jun 6th, 2013 at 5:47 am
how to get product price using Model no from newegg site
[Reply]