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?

3
  • are they provide API? Commented Apr 27, 2013 at 13:11
  • No, I don't think so. Commented Apr 27, 2013 at 13:12
  • Id you want to do it realtime it is hard to using php,jquery .Try to find better API \ Commented Apr 27, 2013 at 13:16

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Oh - sorry, I missed the whole EUR thing. data.mtgox.com/api/2/BTCEUR/money/ticker
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.
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: bitcoincharts.com/about/markets-api For tech info: blockexplorer.com/q
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.
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 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 api.bitcoincharts.com/v1/markets.json
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.

7 Comments

This sounds promising. I edited my question to make clear that caching is explicitly not wanted.
I know that this kind of data must be fresh, but even caching for a very small time, can have a big performance boost.
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?
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.
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.
|
1

Use Document Object Model

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

3 Comments

In how far would this be more efficient? Wouldn't this also fetch the whole site content and pick out what is needed?
Every crawl need whole contents as html-string at once. And Parse data from crawled data. At least, you need whole data at once.
Okay I see, so if there is no API available I will have to fetch the whole site and extract what I need. Thanks for your tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.