In this thread I will demonstrate a quick way to extract the current Bitcoin value from a json stream in PHP using the BlockChain API.
Click [Only registered and activated users can see links. Click Here To Register...]for a preview of what you are going to learn and how the result looks like.
I'm fully aware that this is not rocket-science but this is more of a "noobtorial", targeted towards those who would love to learn PHP and JSON-integration. :)
First step you wanna do is create a PHP document.
Now you start by defining the API you want to use. Since Mt. Gox went down, there is no longer one central Bitcoin exchange and thus there are more sources for rates available.
Next you'd want to get the content of that JSON stream into an object.
After that you'll now need to decode the JSON-stream into its data.
This is how you should proceed:
(Ignore the TRUE for now, this is necessary due to the nature of arrays.)
In the next step you'd want to get the rate out of the data-object you just created. For this step you actually have to know, which currency you wanna get the Bitcoin value in. For this tutorial, I will use EURO. Click [Only registered and activated users can see links. Click Here To Register...] for the actual JSON-stream that shows a list of possible currency abbreviations. The '15min' stands for the average Bitcoin price in the last 15 minutes, you might also take the sell/buy price. Other Bitcoin APIs may vary from this. The usage for the data extraction should be clear from the example.
The actual Bitcoin price is now stored in the variable $rate. To display it, we will use a simple "echo" order. But first I'll show you how to add currency symbols to the output, so that it's a tad more user-friendly. To extract currency symbols make sure your file is saved in UTF-8 or similar and simply add this line:
So as I said the last step would be to print the variables. Simply do this:
For those who don't know "." means AND in a way. So it "echo's" out the variables.
What I've done to make it a tiny bit more user-friendly is to set the font to Verdana. This isn't necessary but you get the point.
Below is the full code:
Thanks for reading, I'll be happy to assist you :)
Click [Only registered and activated users can see links. Click Here To Register...]for a preview of what you are going to learn and how the result looks like.
I'm fully aware that this is not rocket-science but this is more of a "noobtorial", targeted towards those who would love to learn PHP and JSON-integration. :)
First step you wanna do is create a PHP document.
Code:
<?php ?>
Code:
<?php $api = "https://blockchain.info/de/ticker"; ?>
Code:
<?php $api = "https://blockchain.info/de/ticker"; $json = file_get_contents($api); ?>
This is how you should proceed:
(Ignore the TRUE for now, this is necessary due to the nature of arrays.)
Code:
<?php $api = "https://blockchain.info/de/ticker"; $json = file_get_contents($api); $data = json_decode($json, TRUE); ?>
Code:
<?php $api = "https://blockchain.info/de/ticker"; $json = file_get_contents($api); $data = json_decode($json, TRUE); $rate = $data["EUR"]["15m"]; ?>
Code:
<?php $api = "https://blockchain.info/de/ticker"; $json = file_get_contents($api); $data = json_decode($json, TRUE); $rate = $data["EUR"]["15m"]; $symbol = $data["EUR"]["symbol"]; ?>
Code:
<?php $api = "https://blockchain.info/de/ticker"; $json = file_get_contents($api); $data = json_decode($json, TRUE); $rate = $data["EUR"]["15m"]; $symbol = $data["EUR"]["symbol"]; echo "1 Bitcoin equals " . $rate . $symbol; ?>
What I've done to make it a tiny bit more user-friendly is to set the font to Verdana. This isn't necessary but you get the point.
Below is the full code:
Code:
<?php $api = "https://blockchain.info/de/ticker"; $json = file_get_contents($api); $data = json_decode($json, TRUE); $rate = $data["EUR"]["15m"]; $symbol = $data["EUR"]["symbol"]; echo "<div style='font-family:verdana,arial,sans-serif'> 1 Bitcoin equals " . $rate . $symbol . "</div>"; ?>