1

Let's say I want to extract the current bitcoin exchange rate (in EUR) from the German exchange bitcoin.de and the value shall be fetched each time I visit my site (so no caching). I was able to extract the value in PHP:

// fetch contents from bitcoin.de
$url = 'https://www.bitcoin.de/de/';
$content = file_get_contents($url);

// cut everything before specified text
$content = strstr($content, "Aktueller Bitcoin Kurs");

// extract rate
$rate = strstr($content, "<b>");
$rate = substr($rate, 3);
$rate = strstr($rate, "€", true);

echo $rate . " EUR"; // e.g. 105,51 EUR

This works fine and prints the correct current value as it can be found on the bitcoin.de website. But I am fetching the whole website content, substract everything I don't need, and return it.

My question: Is there a way (maybe also using jQuery) to solve this more efficiently; ergo not fetching the whole site code but only the rate value?

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116

3 Answers3

2

You can use maxlen parameter with file_get_contents(). This way you can limit how many characters the function will read. You could also cache the results (store locally) on your server, depending on your needs. In your position I would only cache the results, that will bring most of the performance.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
Rok Burgar
  • 949
  • 5
  • 7
  • This sounds promising. I edited my question to make clear that caching is explicitly not wanted. – Gottlieb Notschnabel Apr 27 '13 at 13:10
  • I know that this kind of data must be fresh, but even caching for a very small time, can have a big performance boost. – Rok Burgar Apr 27 '13 at 13:14
  • Okay, I think the rate is updated each 15 or 30 minutes. I suppose caching would already make sense in this case? What would you suggest? – Gottlieb Notschnabel Apr 27 '13 at 13:15
  • If you need data specifically from this page even caching every 1s is better than making a request for each user. But as someone pointed out, the best way is to make use of an API. – Rok Burgar Apr 27 '13 at 13:17
  • It also depends on the number of users. – Rok Burgar Apr 27 '13 at 13:23
  • What caching would you recommend (e.g. sqlite, file system, ...)? It's only a very small group of people. – Gottlieb Notschnabel Apr 27 '13 at 13:23
  • 1
    Depends on what you have available. The quickest would be redis, mangodb or any other in memory datastructure. But I don't think that write and read speeds are so important here, so sql or filesystem is just fine. The more important thing here is that you don't make a request for every user. – Rok Burgar Apr 27 '13 at 13:28
2

There are many APIs available for accessing that data. While there are way more complex APIs, the information you are looking for is available at

http://data.mtgox.com/api/2/BTCUSD/money/ticker

ScottB
  • 36
  • 1
  • Oh - sorry, I missed the whole EUR thing. http://data.mtgox.com/api/2/BTCEUR/money/ticker – ScottB Apr 27 '13 at 13:16
  • Yeah, USD was not really helpful as I would have to use a currency coverter API. But the Mt.Gox EUR API is great! Thanks. – Gottlieb Notschnabel Apr 27 '13 at 13:21
  • 1
    Great! I dunno exactly what your needs are (update rate, multiple exchanges, etc), but as i said, there are many APIs available for bitcoin data at both the user and developer level. Here are some links: For trade info: http://bitcoincharts.com/about/markets-api/ For tech info: http://blockexplorer.com/q – ScottB Apr 27 '13 at 13:42
  • By the way: I acceptec this as answer although it doesn't fulfill my requirement to fetch this value from bitcoin.de. But as this Mt.Gox API is really good to handle and easy to use I chose to use Mt.Gox instead. – Gottlieb Notschnabel Apr 28 '13 at 10:51
  • 1
    Yeah, I was thinking about that. The bitcoincharts api has the data from bitcoin.de available to you. Unfortunately, bitcoin.de doesn't offer an api (publicly, but it must have one _somewhere_ to interface with bitcoincharts.com). Another option is to use http://api.bitcoincharts.com/v1/trades.csv?symbol=btcdeEUR. That will give you the most recent transactions at bitcoin.de in csv format. You can get the results of all markets as a JSON object (if thats preferable) at http://api.bitcoincharts.com/v1/markets.json – ScottB Apr 29 '13 at 17:39
1

Use Document Object Model

And you need to read it: https://stackoverflow.com/a/3627553/2198378

Community
  • 1
  • 1
jeyraof
  • 863
  • 9
  • 28