How to get Bitcoin price with PHP script

Hello my guests,

in this article I will show you how to get the bitcoin price with PHP and curl. All you will need is a http server with php and php-curl module installed on your machine. No need of api keys or something more. So. let’s go.

In the days of the rising of the crypto currency, there are many crypto trade sites where you can buy or sell bitcoin or other alt coins. Every of this crypto trade sites have their own rates between bitcoin and other alt currencies to the fiat currencies. If you want to watch the price it’s a good idea to know how to do it automaticly. In this article I will show you how to make it with PHP and XAMPP server.

Install HTTPD server and PHP

I will not cover all of this process, because it’s quite easy, so just follow this link and do it by yourself:
https://www.apachefriends.org/download.html

Start coding of Bitcoin price scraper

After the installation is done, you have to test it and if everything is OK, go to the main directory for all WWW documents of the XAMPP, normaly it is \xampp\htdocs.
Delete all unnessesery files there, and leave/make only index.php file. There just write:


<?рhр
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 2,       // stop after 10 redirects
        CURLOPT_SSL_VERIFYPEER => false     // Disabled SSL Cert checks
    );

    $ch      = curl_init( "https://api.coingate.com/v2/rates/merchant/BTC/USD" ); // You can change this to other crypto trade currency rate provider
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch ); // Gets the page's content
    $err     = curl_errno( $ch ); // Gets the CURL error number if there is error
    $errmsg  = curl_error( $ch ); // Gets the CURL error text if there is error
    curl_close( $ch );


    echo date("Y-M-D H:i:s") . " The price of 1 BTC is $". $content . "<br />";


?>

Overall information

This BTC price script is writen in PHP and it works with Coingate, but you can use whatever you want other crypto trade currency rate provider, by just changing the url. Most of them are returning the price of the BTC, according of the link. So just change the link and you are done.
Thanks for your time, and if you have questions to how to get BTC price, just ask in the comments.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x