bf・cc・zaif・btcのビットコイン価格をjsonで取得する(php)

各社が公開しているAPIから、ビットコイン価格をjsonで取得する。凄い時代になったもんだ。

<?php

	$array = array(
	'bf'=>array('name'=>'bitflyer','url'=>'https://api.bitflyer.jp/v1/getticker','ask'=>'best_ask',bid=>'best_bid'),
	'cc'=>array('name'=>'Coin Check','url'=>'https://coincheck.com/api/ticker','ask'=>'ask',bid=>'bid'),
	'zaif'=>array('name'=>'Zaif','url'=>'https://api.zaif.jp/api/1/ticker/btc_jpy','ask'=>'ask',bid=>'bid'),
	'btc'=>array('name'=>'BTCBOX','url'=>'https://www.btcbox.co.jp/api/v1/ticker/','ask'=>'sell',bid=>'buy'),

	);
		
		echo "<table>";
		foreach($array as $exchange){
			$json = file_get_contents($exchange[url]);
			$json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
			$arr = json_decode($json,true);
			echo "<tr><td><strong>".$exchange[name]."</strong></td><td>売り:".number_format($arr[($exchange[ask])]). "円</td><td>買い:".number_format($arr[($exchange[bid])]). "円</td></tr>";
		}
		echo "</table>";
		

?>

json_decode()

連想配列にします。

<?php

$url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=130010";
$json = file_get_contents($url);
$json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$arr = json_decode($json,true);

echo('<pre>');
print_r($arr);
echo('</pre>');
?>

では、東京の今日の天気を表示させてみましょう。

<?php

$url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=130010";
$json = file_get_contents($url);
$json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$arr = json_decode($json,true);

$time = strtotime($arr&#91;description&#93;&#91;publicTime&#93;);

echo "<b>東京:今日の天気</b><br>";
echo $arr[forecasts][0][telop]."<br>";
echo $arr[description]."<br>";
echo date("Y/m/d H:i:s", $time)."<br>";

?>

json_encode

値をJSON形式にて返します。

<?php

$arr = array('BTC'=> 893850, "ETH"=>93612, "XRP"=>89.00, "LTC"=> 13129.58, "DASH"=>58753.52);

echo json_encode($arr);

?>

output
{“BTC”:893850,”ETH”:93612,”XRP”:89,”LTC”:13129.58,”DASH”:58753.52}

では、連想配列でみてみましょう。

<?php

$arr = array(
	"code"=> "2317",
	"name"=> "システナ",
	"price"=>"4570",
	"volume"=>"492500",
	"index"=>array(
		"PER" => "37.0",
		"PBR" => "6.92",
		"rate" => "1.01"
		),
	"url" => "https://www.systena.co.jp/"
);

echo json_encode($arr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

?>

{ “code”: “2317”, “name”: “システナ”, “price”: “4570”, “volume”: “492500”, “index”: { “PER”: “37.0”, “PBR”: “6.92”, “rate”: “1.01” }, “url”: “https:\/\/www.systena.co.jp\/” }

number_format

小数点以下が四捨五入
小数点以下も含むには、第二引数に桁数を記入

<?php
echo number_format(123.45, 1)
?>

面白くないので、連想配列で見てみましょう。

<?php

$array = &#91;
	&#91;"1月30日", 3210,3205&#93;,
	&#91;"1月29日", 3280,3290&#93;,
	&#91;"1月28日", 3285,3310&#93;,
	&#91;"1月27日", 3395,3300&#93;
&#93;;

foreach($array as $trade){
	$dif = number_format(($trade&#91;2&#93; -$trade&#91;1&#93;) / $trade&#91;2&#93;, 4);
	echo $trade&#91;0&#93;. ":" .$dif. "(買値:" .$trade&#91;1&#93;. "、売値:" .$trade&#91;2&#93;.")<br>";
}

?>

Google Analytics Dimensions & Metrics Explorer

こちらにディメンション一覧が掲載されております。
https://developers.google.com/analytics/devguides/reporting/core/dimsmets

例:
ga:users : The total number of users for the requested time period.
ga:newUsers : The number of sessions marked as a user’s first sessions.
ga:1dayUsers : Total number of 1-day active users for each day in the requested time period. At least one of ga:nthDay, ga:date, or ga:day must be specified as a dimension to query this metric. For a given date, the returned value will be the total number of unique users for the 1-day period ending on the given date.

and so on.

使えそうなところとして
-ga:newUsers:The total number of users for the requested time period.
-ga:hits:Total number of hits for the view
-ga:referralPath:The path of the referring URL (e.g., document.referrer). If someone places on their webpage a link to the property, this is the path of the page containing the referring link.
-ga:pageTitle:The page’s title. Multiple pages might have the same page title.
-ga:pageviews:The total number of pageviews for the property.
-ga:avgTimeOnPage:
-ga:date:The date of the session formatted as YYYYMMDD.
-ga:deviceCategory:The type of device: desktop, tablet, or mobile.

ceil()

小数点以下を切り上げます。

<?php

print round(234.567,2)."<br>";
print floor(234.567)."<br>";
print ceil(234.567)."<br>";
?>

消費税計算をしてみます。

<?php

$market1 = 140000;
$market2 = 121824;
$market3 = 99799;

echo "A社のスマートフォン定価は、".$market1."円で、税込み価格は、<mark>端数切り捨て</mark>の".floor($market1*1.08)."円です。<br>";
echo "B社のスマートフォン定価は、".$market2."円で、税込み価格は、<mark>端数切り上げ</mark>の".ceil($market2*1.08)."円です。<br>";
echo "C社のスマートフォン定価は、".$market3."円で、税込み価格は、<mark>端数四捨五入</mark>の".round($market3*1.08, 1)."円です。<br>";
?>

メソッドチェーン2

<?php

class Sum {
	private $sum = 0;
	public static function _(){
		return new self;
	}

	public function add($a){
		$this->sum += $a;
		return $this;
	}
	public function out(&$result){
		$result = $this->sum;
		return $this;
	}
}

Sum::_()->add(1)
		->add(3)
		->out($result1)
		->add(5)
		->add(8)
		->out($result2);
echo "$result1\t$result2";
?>

サンプル2
オークションで例えてみましょう。

<?php

class Auction {
	private $sum=50000;
	public static function _(){
		return new self;
	}

	public function sell($sales){
		$this->sum += $sales;
		return $this;
	}

	public function buy($stock){
		$this->sum -= $stock;
		return $this;
	}

	public function amount(&$result){
		$result = $this->sum;
		return $this;
	}
}

Auction::_()->sell(5000)
			->buy(3000)
			->sell(2000)
			->amount($result1)
			->sell(5000)
			->sell(2000)
			->sell(1000)
			->amount($result2);

echo "$result1\t$result2";
?>

return new self();

<?php

class A {
	public static function get_self(){
		return new self();
	}

	public static function get_static(){
		return new static();
	}
}

class B extends A {}

echo get_class(B::get_self());
echo get_class(B::get_static());
echo get_class(A::get_static());

?>

スコープ定義演算子(::)

<?php

class Box1
{
	public static function hoge(){
		Box2::hoge();
	}
}

class Box2
{
	public static function hoge(){
		echo 'Gold' . PHP_EOL;
	}
}

Box1::hoge();
?>

サンプル2

<?php

class Tse2
{
	public static function reg(){
		self::cap();
	}

	public static function cap(){
		echo "時価総額20億円以上". PHP_EOL;
	}
}

class Tse1 extends Tse2
{
	public static function cap(){
		echo "時価総額250億円以上". PHP_EOL;
	}
}

Tse2::reg();
Tse1::reg();

?>
<?php

class Tse2
{

	public static function cap(){
		echo "時価総額20億円以上". PHP_EOL;
	}
}

class Tse1 extends Tse2
{
	public static function reg(){
		parent::cap();
	}

	public static function cap(){
		echo "時価総額250億円以上". PHP_EOL;
	}
}

Tse1::reg();

?>

メソッドチェーン2

<?php

$abc = '変数abc';
echo $abc, PHP_EOL;
echo ${'abc'}, PHP_EOL;
$name = 'abc';
echo ${$name}, PHP_EOL;
echo $$name, PHP_EOL;

function abc(){
	echo '関数abcが実行されました', PHP_EOL;
}
abc();
$name = 'abc';
$name();
?>
<?php

class Chain {
	public static function _(){
		return new self;
	}

	public function p($name){
		echo $name;
		return $this;
	}
}

Chain::_()->p('hoge')->p('fuga');