User avatar
coinableS
Nickel Bitcoiner
Nickel Bitcoiner
Posts: 65
Joined: Wed Sep 30, 2015 6:06 am

Donate BTC of your choice to 1J9ikqFuwrzPbczsDkquA9uVYeq6dEehsj

Contact: Website Twitter

Learn how to grab the exchange rate and display it on your website.

Tue Nov 10, 2015 6:11 am

PHP is a server-side scripting language. This means that the code is processed by the web server before it is displayed to the user. It is very powerful and is used from small website contact forms to running large social networking websites. JSON is a format for how information is displayed and interpreted. It stands for Java-Script Object Notation. It uses curly braces, commas, quotes, brackets, etc to divide information and make it easily used by scripting languages like PHP.

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"}
This is an example of some JSON with more parameters taken from blockchain.info:

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" },
As you can see there is a lot of data you can get from a JSON output. Keep in mind the last example from blockchain.info is only a small portion of a much larger JSON file. The text inside quotes are identifiers or tags and the data that follows is its value. If I asked for "total_received" the value returned would be 1559028. If asked for "ask" from the first example with bitfinex, the value returned would be 262.23.

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.

Image

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:

Image

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;
If you save this file as a php file and run it, it should display the latest price at Bitfinex. Let's break down this code. The words with $ in front of them are variables. Just like in algebra a variable is just a placeholder that can equal anything.

$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;
Bitstamp

Code: Select all

$url = "https://www.bitstamp.net/api/ticker/"; $json = json_decode(file_get_contents($url), true); $price = $json["last"]; echo $price;
Coinbase

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;
Pretty cool, huh?

https://www.youtube.com/watch?v=_TSJLvYw4p8

Return to “Beginners & Help”

Who is online

Users browsing this forum: No registered users and 3 guests