This is an example of some basic JSON taken from Bitfinex:
Code: Select all
{"mid":"262.035","bid":"261.84","ask":"262.23",
"last_price":"261.77","timestamp":"1425255995.013169392"}
Code: Select all
{ "hash160":"354649cca48f11597f53d5144b69bc651eb41c7e",
"address":"15rh1tAsR2vj3yULB1rmMDQjsJ3j3pjjDm",
"n_tx":2,
"total_received":1559028,
"total_sent":1559028,
"final_balance":0,
"txs":[{
"lock_time":0,
"result":0,
"ver":1,
"size":192,
"inputs":[ {
"sequence":4294967295,
"prev_out":{
"spent":true,
"tx_index":79258139,
"type":0,
"addr":"15rh1tAsR2vj3yULB1rmMDQjsJ3j3pjjDm",
"value":1559028,
"n":0,
"script":"76a914354649cca48f11597f53d5144b69bc651eb41c7e88ac"
},
Let's start with some basic coding. I assume if you are here you have some grasp on working with websites. Whether you are a wizard or novice, I will be going step-by-step. My editor program of choice is Notepad++. You can use whichever editor you like, but you will need one.
In order to execute PHP code you will need to run you files on a PHP server.
Most web hosting providers today run PHP at no additional charge.
Open up your editor, create a new blank file, and let's insert our PHP tags. PHP tags are simply a less-than symbol, followed by a question mark, followed by the letters "P", "H", "P". This will open your PHP tags. To close them is with a question mark followed by a greater-than symbol. Anything written in between the open and close tags will be interpreted as PHP code.

In PHP in order to display text onto the webpage use the command echo. Anything written inside the quotes after an echo will be input onto the screen. For example if you wanted to display the phrase Write on the screen! You would use the below code:

Boring, right? Ok let's get the latest price from Bitfinex and display it on the screen. Bitfinex has public JSON available at https://api.bitfinex.com/v1/ticker/btcusd. Please respect public JSON links and don't not bombard them with 100's of requests per second. Most of them have limits of how often you can hit them within a certain time frame and if you exceed your IP will be banned. Let's open up our PHP tags and within our tags write in this code:
Code: Select all
$url = "https://api.bitfinex.com/v1/ticker/btcusd";
$json = json_decode(file_get_contents($url), true);
$price = $json["last_price"];
echo $price;
$url is being used to define where our JSON string is located on the internet.
$json is using two PHP functions one is json_decode which tells PHP to read this information in JSON format. The other is file_get_contents which tells PHP you have to go here to get the data. Did you notice we input $url right after the file_get_contents? We were telling it to go the previous variable we defined which was Bitfinex's website.
$price is calling the $json variable and then specifying which info from the JSON we want. In this case it is the last price $json["last_price"].
At the end we display the price on the webpage with echo $price;
You can use this same code with just a few tweaks to get the price from other exchanges.
BTC-e
Code: Select all
$url = "https://btc-e.com/api/2/btc_usd/ticker";
$json = json_decode(file_get_contents($url), true);
$price = $json["ticker"]["last"];
echo $price;
Code: Select all
$url = "https://www.bitstamp.net/api/ticker/";
$json = json_decode(file_get_contents($url), true);
$price = $json["last"];
echo $price;
Code: Select all
$url = "https://coinbase.com/api/v1/prices/spot_rate";
$json = json_decode(file_get_contents($url), true);
$price = $json["amount"];
echo $price;
https://www.youtube.com/watch?v=_TSJLvYw4p8