通过雅虎获取历史汇率

$begin = '2015-07-18';
$end = '2015-07-20';
$ts = strtotime($begin);
$end_ts = strtotime($end);

echo "DATE    \t";
$symbolList = array('USD', 'CAD', 'EUR', 'HKD', 'JPY', 'GBP', 'THB', 'AUD');
foreach ($symbolList as $outputSymbol) {
	echo $outputSymbol . "    \t";
}
echo "\n";

do {
	$url = 'https://finance.yahoo.com/connection/currency-converter-cache?date=';
	$data = file_get_contents($url . date('Ymd', $ts));
	$data = substr($data, strpos($data, '(') + 1, -3);
	$data = json_decode($data, true);
	$map = array();
	foreach ($data['list']['resources'] as $res) {
		list($symbol) = explode('=', $res['resource']['fields']['symbol']);
		$map[$symbol] = floatval($res['resource']['fields']['price']);
	}

	echo date('Y-m-d', $ts);
	foreach ($symbolList as $outputSymbol) {
		$price = $map[$outputSymbol];
		if ($outputSymbol != 'USD') {
			$price = round($map['CNY'] / $price, 4);
		} else {
			$price = $map['CNY'];
		}
		printf("\t%.4f",$price);
	}
	echo "\n";

} while (($ts += 86400) <= $end_ts);

输出结果:

发表回复

您的电子邮箱地址不会被公开。