By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

Getting prices from external e-commerce system

P: n/a
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 . '&currency=' . $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/
Oct 6 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Paul Herber wrote:
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 . '&currency=' . $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;
}
?>
A database is definitely NOT overkill, and will cause FEWER reliability
problems.

For instance - any time you open a file for writing (or might have it
open for writing), you need to flock() the file to ensure it isn't
accessed by another script while it is being updated.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 6 '08 #2

P: n/a
On 6 Oct, 11:58, Paul Herber <SubstituteMyFirstNameH...@pherber.com>
wrote:
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 . '&currency=' . $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/
PHPs file locking is not particularly sophisticated and does not scale
well. Also, you need to consider how many files and whether the
filesystem can sensibly cope with this number - many have upper limits
on the number of files, and access times degrades badly for
significant volumes.

Definitely use a database - even if its just dbm or sqlite.

C.
Oct 6 '08 #3

P: n/a
corey wrote:
On Mon, 06 Oct 2008 07:07:16 -0400, Jerry Stuckle <js*******@attglobal.net>
wrote:
>A database is definitely NOT overkill, and will cause FEWER reliability
problems.

For instance - any time you open a file for writing (or might have it
open for writing), you need to flock() the file to ensure it isn't
accessed by another script while it is being updated.


Reliability is only an issue if you dont do things right.

I WOULD like to see some timings for the difference between say
copying a text file - modifying that then updating the original after the floc()
as opposed to the (I suspect) large overhead of using a database for such simple
tasks.

Has anyone ever done any timings?

corey

Not in PHP. It's always an issue. As C indicated, file locking in PHP
isn't the greatest. And you have to ensure you NEVER access the files
unless you lock them first (and unlock them afterwards). Otherwise you
have a bug waiting to occur.

But if you're worried about performance right now - you're prematurely
optimizing.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 6 '08 #4

P: n/a
..oO(corey)
>On Mon, 06 Oct 2008 07:07:16 -0400, Jerry Stuckle <js*******@attglobal.net>
wrote:
>>
A database is definitely NOT overkill, and will cause FEWER reliability
problems.

For instance - any time you open a file for writing (or might have it
open for writing), you need to flock() the file to ensure it isn't
accessed by another script while it is being updated.


Reliability is only an issue if you dont do things right.

I WOULD like to see some timings for the difference between say
copying a text file - modifying that then updating the original after the floc()
as opposed to the (I suspect) large overhead of using a database for such simple
tasks.
Connections to a MySQL database for example are quick and cheap.
I didn't benchmark it, simply because I don't consider it necessary.

Execution speed is not the only thing that matters. If I think that for
a particular task a DB would be the better and more convenient tool,
then I simply use it instead of trying to save some CPU cycles with
another, more complicated and more error-prone method.

Micha
Oct 6 '08 #5

P: n/a
corey wrote:
On Mon, 06 Oct 2008 10:38:47 -0400, Jerry Stuckle <js*******@attglobal.net>
wrote:
>Not in PHP. It's always an issue. As C indicated, file locking in PHP
isn't the greatest. And you have to ensure you NEVER access the files
unless you lock them first (and unlock them afterwards). Otherwise you
have a bug waiting to occur.

If you dont use any function correctly you have a bug waiting. Thats a spurious
arguament.
Not at all. That's why there are bugs.
Are you saying that flock doesnt work (appart from the known ms-windows issues)
If so it needs fixing. If not there is no issue.
Exactly. Lots of known ms-windows issues. And while it works on Linux,
it is very easy to forget to flock() a file before reading it.
>But if you're worried about performance right now - you're prematurely
optimizing.

Not worried just curious about relative performance. If I was really concerned
about performance the last thing I'd consider would be using PHP at all. PHP is
a great scripting language but thats all. For anything much more complex it
isn't suitable.

corey

I don't worry about performance until there is a performance problem.
Instead I use best coding practices - which includes using a database
where one is called for.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Oct 6 '08 #6

P: n/a
Thanks very much, guys.
--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/ http://www.pherber.com/
Oct 6 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.