I'm using the code below to retrieve price data from my e-commerce
provider. The plan being to only have prices stored in one location
and for there to be correct real-time currency conversions.
Using the URL in $url I can get the product price for any quatity or
currency. Initially I was just using fopen to get the data but on
occasion the URL would fail due to timeouts (network congestion,
ecommerce server busy or failure, etc) etc. So now when a good
response has occured I store this in file in the prices directory, if
a failure occurs in the future then I can use the stored price
instead. Typically, my web pages would display prices in pounds,
dollars and euro. Traffic levels aren't very high, maybe 100 hits per
day.
Can anyone see any problems here or are there better ways of doing
this? I think a database would be overkill for this and may even cause
more reliability problems than I'm trying to fix.
MTIA
<?php
function ContractPriceForCurrency($contract, $quantity, $currency) {
$filename = 'prices/' . $contract . $quantity . $currency .
'.txt';
$url = 'http://www.xxxx.com/jsp/get_local_price.jsp?contractId=' .
$contract . '&quantity=' . $quantity . '¤cy=' . $currency .
'&responseType=number';
$c = curl_init($url);
curl_setopt($c, CURLOPT_TIMEOUT, 1);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$price = curl_exec($c);
$price = trim($price);
curl_close($c);
// if a price has been returned then store it and use it
if ($price !== '') {
$fd = fopen($filename,"w");
fwrite($fd, $price);
fclose($fd);
} else {
// otherwise use the stored value
$fd = fopen($filename,"r");
$price = fread($fd, filesize($filename));
fclose($fd);
$price = trim($price);
}
return $price;
}
?>
--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/ http://www.pherber.com/