3
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 5 years have passed since last update.

外部API情報をPHP curlを使ってビットコイン価格だけ取得

3
Last updated at Posted at 2019-09-01

bitFlyerのパブリックAPIからビットコイン価格だけをcurlを使って取得します。

【bitflyer各種情報が確認できるURL】
https://api.bitflyer.jp/v1/ticker?product_code=BTC_JPY

2019/9/1現在の表示内容

{"product_code":"BTC_JPY","timestamp":"2019-09-01T12:23:35.213","tick_id":2741559,"best_bid":1019318.0,"best_ask":1019938.0,"best_bid_size":0.01799886,"best_ask_size":0.01200473,"total_bid_depth":853.24377747,"total_ask_depth":2064.70810122,"ltp":1019318.0,"volume":49508.95318553,"volume_by_product":1568.33670575}

この中にBitcoinの価格が表示されています。

"ltp":数字

の「数字」部分がBitcoin価格。

curlを使ってこの箇所だけを抜き出し、整形します。

curl.php
<?php
$url = 'https://api.bitflyer.jp/v1/ticker?product_code=BTC_JPY';

//curlセッション初期化
$ch = curl_init();

//オプション設定
curl_setopt($ch, CURLOPT_URL, $url);//取得するURLを指定
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//実行結果を文字列で返す

//URLの情報を取得
$result = curl_exec($ch);

//結果の表示
$json = json_decode($result);

echo '現在のBTC価格は:' . number_format($json->ltp) . '円';

//セッションを終了
curl_close($ch);
?>

するとブラウザ上では
スクリーンショット 2019-09-01 21.52.09.png
とだけ表示されます。

3
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
3
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?