Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Coding Releases > Coding Snippets
You last visited: Today at 11:00

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[PHP] Get Bitcoin value from BlockChain API

Discussion on [PHP] Get Bitcoin value from BlockChain API within the Coding Snippets forum part of the Coding Releases category.

Reply
 
Old   #1
 
chrisyou's Avatar
 
elite*gold: 103
Join Date: Jun 2009
Posts: 1,972
Received Thanks: 410
[PHP] Get Bitcoin value from BlockChain API

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 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

?>
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.

Code:
<?php
$api = "https://blockchain.info/de/ticker";
?>
Next you'd want to get the content of that JSON stream into an object.

Code:
<?php
$api = "https://blockchain.info/de/ticker";

$json = file_get_contents($api);
?>
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.)

Code:
<?php
$api = "https://blockchain.info/de/ticker";

$json = file_get_contents($api);
$data = json_decode($json, TRUE);
?>
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 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.

Code:
<?php
$api = "https://blockchain.info/de/ticker";

$json = file_get_contents($api);
$data = json_decode($json, TRUE);
$rate = $data["EUR"]["15m"];
?>
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:

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"];
?>
So as I said the last step would be to print the variables. Simply do this:

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;
?>
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:

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>";
?>
Thanks for reading, I'll be happy to assist you
chrisyou is offline  
Old 12/09/2014, 22:11   #2

 
snow's Avatar
 
elite*gold: 724
Join Date: Mar 2011
Posts: 10,479
Received Thanks: 3,318
Arrow Coding Tutorials -> Coding Snippets

#moved
snow is offline  
Old 12/10/2014, 15:24   #3
 
elite*gold: 25
Join Date: Sep 2011
Posts: 5,536
Received Thanks: 1,266
I would rather to cache it because:
Quote:
Anfragen in 8 Stunden: 4 (Soft Limit = 28800, Hard Limit = 28900)
Anfragen in 5 Minuten: 4 (Soft Limit = 700, Hard Limit = 725)
Its a high limit, but you might reach it with a big website.
IchVerabschiedeMich is offline  
Reply


Similar Threads Similar Threads
[Selling] 81€ BTC (Blockchain) [S] PayPal/Banküberweisung
12/21/2014 - Trading - 6 Replies
Moin, verkaufe hier meine 81€ BTC für PayPal/Banküberweisung. Meldet euch einfach, wenn ihr Interesse habt.
Bitcoin gegen Hashing power - Günstiger Bitcoin Cloud Mining Hoster
12/11/2014 - Freebies - 3 Replies
Hey! Auf der Suche nach einem fairen Bitcoin Cloud Mining Hoster bin ich auf folgende Seite gestoßen: Cloudmining-review.com/ Dort ist eine Übersicht mit Anbietern. Dadurch bin ich auf cloudhashinsercives gekommen echt ne tolle Seite! Dort kann man seine Bitcoins gegen Hashingpower tauschen. Der Preis für 1 GH/s beträgt 0.0015 BTC. Der Vertrag läuft für 5 Jahre und es fallen keine weiteren Kosten an. Die Auszahling findet täglich statt. Bis jetzt habe ich nur gutes gelesen. Das erste...
[S] Bitcoin & Perfect Money Cashin für's Sahnescirpt [B] Bitcoin/Bargeld/egold
07/24/2013 - elite*gold Trading - 0 Replies
Heyo, benötige ein Bitcoin und Perfect Money Cashin für's Sahnescirpt. Bitcoin ist erst einmal primär. Kann mit Bitcoin oder Bargeld zahlen. Optional auch egold. Überweisung und Paypal kann ich aus Sicherheitsgründen nicht bieten.
[S] Bitcoin & Perfect Money Cashin für's Sahnescirpt [B] Bitcoin/Bargeld
07/24/2013 - Coders Trading - 0 Replies
Heyo, benötige ein Bitcoin und Perfect Money Cashin für's Sahnescirpt. Bitcoin ist erst einmal primär. Kann mit Bitcoin oder Bargeld zahlen. Optional auch egold. Grüße;
Suche BTC Bitcoin biete Paypal Searching Bitcoin / BTC
11/22/2012 - Cryptocurrency Trading - 1 Replies
German/ Deutsch Suche Bitcoin English/ Englisch Searching for Bitcoin



All times are GMT +1. The time now is 11:00.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.