-
Notifications
You must be signed in to change notification settings - Fork 0
/
StockCheckFunctions.php
135 lines (102 loc) · 2.89 KB
/
StockCheckFunctions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
require_once('secret.php');
function displayItemInfo($productId)
{
$infoFileName = "data.csv";
$productCode = getProductCode($productId);
$productUrl = getProductUrl($productCode);
$productPage = getProductHtmlCode($productUrl);
if (!doesProductExist($productPage))
{
//item does not exist!
echo "No item found matching product ID\n";
echo "<br />";
return false;
}
$infoFile = fopen($infoFileName,"a+");
fputcsv ($infoFile , array($productId, time()), ',');
fclose ($infoFile);
$productPageTitle = get_page_title($productPage);
//echo "PRODUCT PAGE TITLE: ". $productPageTitle;
$productTitle = getProductName($productPageTitle);
//echo "PRODUCT TITLE: ". $productTitle;
$productPrice = get_product_price($productPage);
//echo "PRODUCT PRICE: ". $productPrice;
$productImage = get_product_image($productId);
//echo "PRODUCT IMAGE: ". $productImage;
echo ' <table width="800">
<tr>
<td align="center" width="375">
<a href="'.$productUrl.'">Buy '.$productTitle.' - €'.$productPrice.'</a>
</td>
<td align="center">
<img src="'.$productImage.'" alt="'.$productTitle.'" />
</td>
</tr>
</table>';
echo "<br /><br />";
return true;
}
function getProductCode($productId)
{
$obj = secretGetJsonStockObject($productId, "I015");
return $obj[0] -> {'ProductId'};
}
function getProductHtmlCode($productUrl)
{
return file_get_contents($productUrl);
}
function getProductUrl($productCode)
{
$productUrl = "http://www.smythstoys.com/ie/en-ie/product/".$productCode."/";
return $productUrl;
}
function doesProductExist($productPage)
{
$pos = strpos($productPage, "The page you are looking for cannot be found");
if ($pos !== false)
{
echo "No item found matching product ID\n";
echo "<br />";
return false;
}
else
{
//echo "PRODUCT EXISTS";
return true;
}
}
function getProductName($productPageTitle)
{
//$tempArray = explode (" – Top Toy Store in UK & Ireland, Games & Toys Online from Smyths Superstores", $productPageTitle);
//$productName = $tempArray[0];
return $productPageTitle;
}
function get_page_title($data)
{
// Get <title> line
preg_match ("/<title>([^`]*?)<\/title>/", $data, $match);
//error_log (var_dump($match));
//error_log($data);
return $match[1];
}
function get_product_price($data)
{
// Get price
//preg_match ("/<h2>.{1}([^<]*)</", $data, $match);
//preg_match ("/<h2>\D*([0-9]*\.{1}[0-9]{0,2})</", $data, $match);
preg_match ("/<div class=\"price normal\">\D*([0-9]*\.{1}[0-9]{0,2})<\/div>/", $data, $match);
if(count($match) > 1)
{
return $match[1];
}
//Sale Price:
preg_match ("/<span class=\"price\">\D*([0-9]*\.{1}[0-9]{0,2})<\/price>/", $data, $match);
}
function get_product_image($productId)
{
// Get Image
//http://d1whee3s2ff61n.cloudfront.net/product/extralarge/131777.jpg
return "http://d1whee3s2ff61n.cloudfront.net/product/large/".$productId.".jpg";
}
?>