Querying upcitemdb.com
Request url: https://api.upcitemdb.com/prod/trial/lookup?upc=885909918188
Response body:
{
"code": "OK",
"total": 1,
"offset": 0,
"items": [
{
"ean": "0885909918188",
"title": "Apple MacBook Pro 13.3" Retina Display Notebook Computer 2.7GHz Dual-core Intel Core i5 (Broadwell) 8GB DDR3 RAM 256GB PCIe Flash Storage (Early 2015)",
"description": "A Spectacular display is just the beginning A groundbreaking Retina display. A new force-sensing trackpad. All-flash architecture. Powerful dual-core and quad-core Intel processors. Together, these features take the notebook to a new level of performanc",
"upc": "885909918188",
"brand": "Apple",
"model": "MF840LL/A",
"color": "Silver",
"size": "13.3in. Retin A Display",
"dimension": "",
"weight": "",
"currency": "",
"lowest_recorded_price": 250,
"images": [
"http://images.highspeedbackbone.net/SKUimages/large/40300998_vmain01_at_mn_4719591.jpg",
"https://i5.walmartimages.com/asr/19d6cafd-e520-4a6f-b6c3-3ddcbc2cbfe5_1.b191d88d51a33125c7334c23ccafd17c.jpeg?odnHeight=450&odnWidth=450&odnBg=ffffff",
"http://img.bbystatic.com/BestBuy_US/images/products/8532/8532029_sc.jpg",
"http://img1.r10.io/PIC/100026633/0/1/250/100026633.jpg",
"http://images.frys.com/art/product/300x300/8408658.01.prod.jpg",
"http://image1.cc-inc.com/prod/13446000/13446712_xlg.jpg",
"http://images.qvc.com/is/image/e/03/e282303.001?$uslarge$",
"http://images10.newegg.com/ProductImageCompressAll200/A5E6_1_20150416344022063.jpg",
"http://www.adorama.com/images/large/ACMF840LLA.JPG",
"http://images.prosperentcdn.com/images/250x250/content.abt.com/media/images/products/l_MF841LLA.jpg"
],
"offers": [
{
"merchant": "Abt",
"domain": "abt.com",
"title": "Apple MacBook Pro 13.3\"\" With Retina Display 2.7GHz Intel Core i5 Laptop Computer",
"currency": "",
"list_price": "",
"price": 1463.7,
"shipping": "",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2s233230363b4d4y2&tid=1&seq=1495966973&plt=a91fdcbd73c8bd4e60a9714aeee31a9d",
"updated_t": 1465780889
},
{
"merchant": "Adorama",
"domain": "adorama.com",
"title": "Apple MacBook Pro 13.3" Retina Display Notebook Computer 2.7GHz Dual-core Intel Core i5 (Broadwell) 8GB DDR3 RAM 256GB PCIe Flash Storage (Early 2015)",
"currency": "",
"list_price": "",
"price": 1399,
"shipping": "Free Shipping",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2q2y2t20333f4d4v2&tid=1&seq=1495966973&plt=3a991c30da14501f7312723dad5d133c",
"updated_t": 1474863552
},
{
"merchant": "Newegg.com",
"domain": "newegg.com",
"title": "Apple MacBook Pro Core i5 2.7GHz (Broadwell) 256GB SSD 8GB 13.3\" Retina (2560x1600) BT Mac OS X 10.10 Yosemite FaceTime Camera (Early 2015)",
"currency": "",
"list_price": "",
"price": 1400,
"shipping": "19.99",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=v2p213x2z213e4d4v2&tid=1&seq=1495966973&plt=deb4f6650100517f14a271418d220b45",
"updated_t": 1479210487
},
{
"merchant": "QVC.com",
"domain": "qvc.com",
"title": "Apple 13\" MacBook Pro - Core i5, 8GB RAM, 256GB SSD & Software",
"currency": "",
"list_price": 2449.96,
"price": 2229.96,
"shipping": "26.22",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2q213u2w21394c4z2&tid=1&seq=1495966973&plt=40ad7a815d023aff6248c1274a3e0047",
"updated_t": 1495194894
},
{
"merchant": "MacMall",
"domain": "macmall.com",
"title": "Apple MF840LL/A 13.3 MacBook Pro with Retina display Dual-core Intel Core i5 2.7GHz (5th generation processor) 8GB RAM 256GB PCIe-based flash storage Intel",
"currency": "",
"list_price": 1499,
"price": 1449,
"shipping": "0",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2q2y2u203y2a484s2&tid=1&seq=1495966973&plt=05ee68a4c8840f1d1d98683c2b4fd5ea",
"updated_t": 1493828651
},
{
"merchant": "Frys Electronics",
"domain": "frys.com",
"title": "NEW! Apple 13.3\" MacBook Pro with Retina display, Dual-core Intel Core i5 2.7GHz (5th generation processor), 8GB RAM, 256GB PCIe-based flash storage, Intel Iris Graphics 6100, Two Thunderbolt 2 ports, 802.11ac Wi-Fi, 10 hours of battery life, OS X Yosemit",
"currency": "",
"list_price": "",
"price": 826.2,
"shipping": "",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2r2y2u2w2z29454u2&tid=1&seq=1495966973&plt=71b0016c76923334976e1e1276fcb654",
"updated_t": 1488643982
},
{
"merchant": "Rakuten(Buy.com)",
"domain": "rakuten.com",
"title": "Apple MacBook Pro 13-inch - Retina 2.7Ghz 8GB 256GB - Mar 2015 / MF840LL/A Apple MacBook Pro MF840LL/A 13.3-Inch Laptop with Retina Display (256 GB) NEWEST VERSION",
"currency": "",
"list_price": "",
"price": 1456.39,
"shipping": "Free Shipping",
"condition": "New",
"availability": "Out of Stock",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2q2z2x233z2d454r2&tid=1&seq=1495966973&plt=99f13618f01e1dff11e4be71651ba804",
"updated_t": 1495830797
},
{
"merchant": "Best Buy",
"domain": "bestbuy.com",
"title": "Apple - Macbook Pro With Retina Display (latest Model) - 13.3\" Display - 8gb Memory - 256gb Flash Storage - Silver",
"currency": "",
"list_price": "",
"price": 1499.99,
"shipping": "",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2q2y2y243z2a494t2&tid=1&seq=1495966973&plt=675f0e0e62217e008130da95bf5559c4",
"updated_t": 1486163626
},
{
"merchant": "Wal-Mart.com",
"domain": "walmart.com",
"title": "Apple MacBook Pro 13\" Retina Display MF840LLA Intel Core i5 2.7GHz 8GB 256GB SSD",
"currency": "",
"list_price": "",
"price": 2499.99,
"shipping": "Free Shipping",
"condition": "New",
"availability": "Out of Stock",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2r2x2v223z264d4u2&tid=1&seq=1495966973&plt=68d38043ead4b8c58b21e94c52f8bf48",
"updated_t": 1495839438
},
{
"merchant": "TigerDirect",
"domain": "tigerdirect.com",
"title": "Apple MacBook Pro - MF840LL/A",
"currency": "",
"list_price": "",
"price": 1494,
"shipping": "",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=v2p24323y2637464s2&tid=1&seq=1495966973&plt=66b097fc123d469aeb62b2848a4808f0",
"updated_t": 1495919811
},
{
"merchant": "Blinq.com",
"domain": "blinq.com",
"title": "Apple MacBook Pro 13.3in Laptop i5 3.1GHz 8GB 512GB Mac OS X (MF843LL/A)",
"currency": "",
"list_price": "",
"price": 1510.39,
"shipping": "",
"condition": "New",
"availability": "",
"link": "http://www.upcitemdb.com/norob/alink/?id=u2x2531323y2c464x2&tid=1&seq=1495966973&plt=0124fecae18fb0c7ba7597c53ba0280c",
"updated_t": 1478056130
},
{
"merchant": "Newegg Business",
"domain": "neweggbusiness.com",
"title": "Apple MacBook Pro Core i5 2.7GHz (Broadwell) 256GB SSD 8GB 13.3\" Retina (2560x1600) BT Mac OS X 10.10 Yosemite FaceTime Camera (Early 2015)",
"currency": "",
"list_price": "",
"price": 1527.69,
"shipping": "Free Shipping",
"condition": "New",
"availability": "Out of Stock",
"link": "http://www.upcitemdb.com/norob/alink/?id=v2o263v2y2337464z2&tid=1&seq=1495966973&plt=4a640983f6c43696ba10ec5c19027b3b",
"updated_t": 1495894981
}
],
"elid": "152538538811"
}
]
}
How we fetch this API
Make a request to the API and store the result
$client = new \GuzzleHttp\Client(array(
'curl' => array(
CURLOPT_SSL_VERIFYPEER => env('CURLOPT_SSL_VERIFYPEER') ,
) ,
));
$request = $client->request('GET', 'https://api.upcitemdb.com/prod/trial/lookup', ['query' => 'upc=' . $request->upc_code]);
$res = $request->getBody();
$data = json_decode($res, true);
$items = $data['items'];
We then fetch downloaded data, parsing each product in result.
// print_r($items);
foreach($items as $item)
{
$product = \App\Product::firstOrNew(['ean_code' => $item['ean']]);
$product->name = $item['title'];
$product->brand = $item['brand'];
$product->description = $item['description'];
$product->lowest_recorded_price = $item['lowest_recorded_price'];
// check if an array of images exists first
// fix for https://github.com/adrianharabula/condr/issues/169
if ($item['images'])
{
$product->image_url = $item['images'][0];
}
// insert category "none" if the user doesn't specify it
$product->category_id = '9';
$product->save();
After saving product to database, attach characteristics to it.
if ($item['color'])
{
$cistic = \App\Characteristic::firstOrCreate(['name' => 'color']);
// alternative way of inserting custom data into pivot
// $pivot_data = ['cvalue' => $item['color']];
// $data_to_sync[$product->id] = $pivot_data;
// $cistic->products()->syncWithoutDetaching($data_to_sync);
$cistic->products()->syncWithoutDetaching([$product->id => ['cvalue' => $item['color']]]);
$cistic->save();
}
if ($item['size'])
{
$cistic = \App\Characteristic::firstOrCreate(['name' => 'size']);
$cistic->products()->syncWithoutDetaching([$product->id => ['cvalue' => $item['size']]]);
$cistic->save();
}
if ($item['dimension'])
{
$cistic = \App\Characteristic::firstOrCreate(['name' => 'dimension']);
$cistic->products()->syncWithoutDetaching([$product->id => ['cvalue' => $item['dimension']]]);
$cistic->save();
}
if ($item['weight'])
{
$cistic = \App\Characteristic::firstOrCreate(['name' => 'weight']);
$cistic->products()->syncWithoutDetaching([$product->id => ['cvalue' => $item['weight']]]);
$cistic->save();
}
Lastly check if the product has any offers and attach them to using one to many association.
foreach($item['offers'] as $offer)
{
$offer_model = \App\Offer::firstOrNew(['merchant' => $offer['merchant'], 'product_id' => $product->id]);
$offer_model->domain = $offer['domain'];
$offer_model->title = $offer['title'];
$offer_model->currency = $offer['currency'];
$offer_model->price = $offer['price'];
$offer_model->shipping = $offer['shipping'];
$offer_model->condition = $offer['condition'];
$offer_model->availability = $offer['availability'];
$offer_model->shop_link = $offer['link'];
$offer_model->remote_updated_at = $offer['updated_t'];
// associate offer with product
$offer_model->product()->associate($product);
$offer_model->save();
}
}