我想用PHP制作一个函数,将比特币转换为usd,这是我的代码:
<?php
function usd2btc($btc) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "spider",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => false
);
$ch = curl_init( "https://api.coingate.com/v2/rates/merchant/BTC/USD" ); //BTC2USD exange
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 );
if ($btc !== 0) {
return round($btc/$content, 2);
} else {
return 0.00;
}
}
?>
例如,当我运行函数时:
<p>0.002฿ = <?php htmlspecialchars(usd2btc(0.002)); ?>$</p>
它不返回任何东西,甚至不返回0。你们有一些解决方案吗?帮助。