473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Getting prices from external e-commerce system

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
6 1361
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
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
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
..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
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
Thanks very much, guys.
--
Regards, Paul Herber, Sandrila Ltd.
http://www.sandrila.co.uk/ http://www.pherber.com/
Oct 6 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hugh Macdonald | last post by:
I'm calling a command from within a python script and I need to be able to both catch the output (stdout and stderr) from it and also have the PID (so that I can kill it) I can do one or other...
2
by: john smith | last post by:
Hello, I am having trouble getting the parent node of an xml document using xslt. I am trying to return the node wine, but this does not return wine but the element values under wine. I...
0
by: get.certified | last post by:
Hi there... Hope you will be doing fine. Actually, we offer Services regarding Testing of Certification of Microsoft, CISCO, ORACLE, CompTia (A+), MacroMedia, CheckPoint, and many other...
2
by: N. Graves | last post by:
I'm planning to develop a process to import data from an external flat file to a new table automatically. As usual I'm having a hard time getting started on the solution because of the Access's...
15
by: Colin | last post by:
I have a query that calculates the selling price of products on customer orders. Selling prices are calculated based on the average cost of the items when purchased. As I make new purchases, the...
13
by: Ørjan Langbakk | last post by:
I wanna make a file that holds the complete pricelist for a small webshop (yes, I know that a database in the background would be a lot simpler, but that is not an option today, unfortunately). ...
7
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i noticed when i run my stored procedure in sql studio i get a result set from my select statement and then below that i get a return value. how about when it's called from inside csharp....
0
by: rich | last post by:
Hi all, I have a fairly complex "feed" application that recieves messages from an external user-supplied API via a callback function, and attempts to forward these messages to another...
6
by: Pete Kane | last post by:
Hi All, does anyone know a site that I can query share prices programmatically ( possibly passing in a stock symbol )?
0
by: service0043 | last post by:
Some bargain prices can be found on jewelry that is sold in bulk and at auction houses on the internet. While the condition of the items might be used, some fine jewelry pieces will be sold as new....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.