How to grab BTC price from bittrex.com using file_get_content() PHP function

in #utopian-io8 years ago (edited)

What Will I Learn?

  • You will learn about bittrex api
  • You will learn about file_get_content() PHP function
  • You will learn how to display specifies data from API on our web page

Requirements

  • You have basic about HTML and PHP
  • You must have a webserver to run php file
  • You must have a text editor software and a browser

Difficulty

  • Basic

Tutorial Contents

file_get_content() function is one of ways to grab data from other web and display it on our web. This function is the preferred way to read the contents of a file into a string. Because it will use memory mapping techniques, if this is supported by the server, to enhance performance.

To grab data from a web we need a permission to access it API. Usually, The big website like bittrex.com always provide the API for other developer. There is Public API that can access publicly and private api that need private key to access.

Here some steps to grab BTC price from bittrex.com :

  • ReActivate your Server, Then create a php file and save it on Xampp/htdocs/ if you use XAMPP. While you use Linux save at var/www with the name btc.php or with other name up to you(must php extension).
  • Before using file_get_content() method we need hava an API for BTC price from bittrex.com . To get more API from bittrex you can visit this link https://bittrex.com/home/api . There are many kind of API you will get. In this cases we need the BTC price in USD, so we use this API
https://bittrex.com/api/v2.0/pub/currencies/GetBTCPrice
  • Open btc.php` file with your text editor, then start to write php code.
<?php
....
?>
  • Create a php variable in with the value is file_get_content() method and the content is the API.
 $api=file_get_contents("https://bittrex.com/api/v2.0/pub/currencies/GetBTCPrice");
  • Print the result, then try to run your btc.php file on your browser. If succes you will the data about BTC price in USD.
echo $api;

image.png

  • The Data still in string, To get the specific data we should convert it first to array with json_decode() method the use var_dump() function to get the varible type of the data
$data=json_decode($api,true);
 var_dump($data);

image.png

  • Because we don't need all data, just the price of BTC in USD we should split the data with array.
    Split 1
    $split1=$data["result"];
    var_dump($split1);


Split 2

$split2=$split1["bpi"];
 var_dump($split2);


Split 3

$split3=$split2["USD"];
    var_dump($split3);

Split 4
Call the index with the value is the BTC price, it is ["rate"]. then Print the result

    $price=$split3["rate"];
    echo $price;

image.png

  • The important data that we need has get by us. Now, we need display it in html element as we want
echo '<center>
            <h3>The BTC price today according bittrex.com</h3>
            <h1> $ '.$price.'</h1>
        </center>';
  • Finish, you can modify the display as you want ( this just for example)

  • For the full code you can get download here or you can also copy bellow

<?php
    $api=file_get_contents("https://bittrex.com/api/v2.0/pub/currencies/GetBTCPrice");
    $data=json_decode($api,true);
    $split1=$data["result"];
    $split2=$split1["bpi"];
    $split3=$split2["USD"];
    $price=$split3["rate"];
    echo '<center>
            <h3>The BTC price today according bittrex.com</h3>
            <h1> $ '.$price.'</h1>
        </center>';
?>

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @shreyasgune, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

bg @sogata... sepertinya bittrex sudah tidak bisa daftar lgi ya bg ?

sepertinya iya dek. Kawan saya juga bilang gitu. Tapi saya belum pernah coba

a very good post and must have a certain skill can make like that

Please upvote me, me upvote yoe

Hey @sogata I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.077
BTC 63686.31
ETH 1660.93
USDT 1.00
SBD 0.42