5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ビットコインと円のレートの取得

Last updated at Posted at 2017-06-25

api.bitflyer.jp から、ビットコインのレートを取得するサンプルです。
python3,php7,node.js のサンプルです。

get_rate.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	get_rate.py
#
#					Jun/25/2017
# --------------------------------------------------------------------
import sys
import requests

# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
url='https://api.bitflyer.jp/v1/ticker?product_code=BTC_JPY'
rr=requests.get(url)
dict=rr.json()
#
print('timestamp = ' + dict['timestamp'])
print('ltp = ' + str(dict['ltp']))
print('best_bid = ' + str(dict['best_bid']))
print('best_ask = ' + str(dict['best_ask']))
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
get_rate.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//	get_rate.php
//
//					Jun/25/2017
//
// ------------------------------------------------------------------
function curl_get_proc ($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_PROXY, "");

$data = curl_exec($ch);

curl_close($ch);

return $data;
}

// ------------------------------------------------------------------
fputs (STDERR,"*** 開始 ***\n");
$url_in='https://api.bitflyer.jp/v1/ticker?product_code=BTC_JPY';
$json_string = curl_get_proc ($url_in);
$dict_aa = json_decode ($json_string,true);
print "timestamp = " . $dict_aa['timestamp'] . "\n";
print "ltp = " . $dict_aa['ltp'] . "\n";
print "best_bid = " . $dict_aa['best_bid'] . "\n";
print "best_ask = " . $dict_aa['best_ask'] . "\n";
fputs (STDERR,"*** 終了 ***\n");
// ------------------------------------------------------------------
get_rate.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	get_rate.js
//
//					Jun/25/2017
// ---------------------------------------------------------------
console.log ("*** 開始 ***")

var https = require('https')

const url_in='https://api.bitflyer.jp/v1/ticker?product_code=BTC_JPY'
https.get(url_in, function(res) {
	res.on('data', function(dd)
	{
	json_str = dd.toString()
	dict_aa = JSON.parse(json_str)
	console.log ("timestamp = " + dict_aa["timestamp"])
	console.log ("ltp = " + dict_aa["ltp"])
	console.log ("best_bid = " + dict_aa["best_bid"])
	console.log ("best_ask = " + dict_aa["best_ask"])
	console.log ("*** 終了 ***");
	})
	}).on('error', function(ee)
		{
		console.error(ee)
		})

// ---------------------------------------------------------------

次のバージョンで動作を確認しました。

$ python --version
Python 3.9.5

$ php --version
PHP 7.4.16 (cli) (built: Mar 23 2021 16:15:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies

$ node --version
v14.14.0
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?