我正在建立一个零售企业,使用 opencart 通过互联网销售黄金。不同之处在于我只接受比特币(BTC),一种新的加密货币。
问题是贵金属价格和 BTC/GBP 交易所非常不稳定。因此,我需要我的 opencart 价格以相当频繁的速度自动更新,以避免因汇率变动而陷入困境并亏本出售。
我编写了一个附加的脚本,该脚本可以提取黄金、白银和铂金的价格,然后以比特币计价。
因此,对于网站上的每个金属物品,我希望目录中所有产品的价格自动更改(每 15 分钟一次比较理想)。
我的问题是:如何根据脚本更新 opencart 数据库中的价格?
价格必须根据四件事改变:物品所含贵金属的类型。该项目的百分比标记该项目包含的贵金属重量。物品的硬币类型。
我希望这些东西成为可在 opencart 中编辑的属性。对于每个项目,将有四个(额外)属性
金属类型 => 黄金 => 银 => 铂金 => 不是金属(对于其他物品,它会忽略所有重量、价格变化) 百分比标记百分比(整数?)重量整数硬币标记整数“所以,假设我' m 出售 1/4 盎司克鲁格朗型金币:
四分之一盎司金币重 7.77 克,因此在产品页面上,重量会与该数字一起列出。基本价格将设置为 7.77 克 * 以克为单位的黄金价格(这个数字是使用附加的 php 脚本获取的)。
我的批发黄金价格的默认加价为 10%,“基本”价格将增加 10%,这也将列在产品页面上。
由于历史原因,Krugerrands 倾向于包含超过黄金批发价的价值。所以硬币加价大约是 5 个比特币,价格会上涨以反映这一点。" 买家不应该看到这些价格变动,他们应该只看到价格变化以反映所有这些
每小时所有产品的价格都会发生变化,以反映黄金价格和 BTC 英镑汇率的变化。
下面是一个 PHP 脚本,用于提取黄金价格、白银价格和铂金价格,然后以比特币计价一克金属的价格。这是我编程过的第一件事,所以可能有点冗长,或者不遵循给定的约定。答案可能很复杂,所以我不指望一个完整的。很高兴收到一些指示!
<?php
/* This is a script that ultimately displays the price of gold in grams, denominated in Bitcoins (BTC), a non fiat cryptographic currency. This is also the first thing I have ever programmed, so be nice */
// line break variable
$br = "<br> <br>";
// CURL begin
//curl session 1: grab metal prices
$ch1 = curl_init();
curl_setopt( $ch1, CURLOPT_URL, 'http://drayah.no.de/metals/latest' );
curl_setopt( $ch1, CURLOPT_RETURNTRANSFER, 1 );
$metalsdata = curl_exec( $ch1 );
// curl session 2: Grab exchange rate for BTC/GBP
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://mtgox.com/api/1/BTCGBP/public/ticker' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt( $ch, CURLOPT_TIMEOUT_MS, 1000);
curl_setopt( $ch, CURLOPT_USERAGENT, 'useragent');
$btcgbpdata = curl_exec( $ch );
curl_close( $ch );
// curl session 3: Grab British pounds to USD exchange rates
$ch1 = curl_init();
curl_setopt( $ch1, CURLOPT_URL, 'https://raw.github.com/currencybot/open-exchange-rates/master/latest.json' );
curl_setopt( $ch1, CURLOPT_Rcheck if my code isETURNTRANSFER, 1 );
$currencydata = curl_exec( $ch1 );
//gold digest: turn JSON data into readable php, and extract the gold price
$metals = json_decode( $metalsdata, true, 512 );
$goldprice = $metals['gold']['quote'];
$silverprice = $metals['silver']['quote'];
$platprice = $metals['platinum']['quote'];
//BTC digest of JSON
$btcgbp = json_decode( $btcgbpdata, true, 512 );
$btcgbpvwap = ($btcgbp['return']['vwap']['value']);
echo "$btcgbpvwap pounds per BTC";
echo $br;
//currency digest of JSON
$currency = json_decode( $currencydata, true, 512 );
$usdgbp = ($currency['rates']['GBP']);
echo "$usdgbp pence per USD dollar";
echo $br;
echo $br;
// convert metals into price per ounce in pounds
$goldpounds = $goldprice * $usdgbp;
$silverpounds = $silverprice * $usdgbp;
$platpounds = $platprice * $usdgbp;
echo "GBP $goldpounds per ounce of gold";
echo $br;
echo "GBP $silverpounds per ounce of silver";
echo $br;
echo "GBP $platpounds per ounce of platinum";
echo $br;
echo $br;
//metals prices in grams
$goldpoundsgram = $goldpounds / 31.1034768;
$silverpoundsgram = $silverpounds / 31.1034768;
$platpoundsgram = $platpounds / 31.1034768;
echo "$goldpoundsgram pounds per gram of gold";
echo $br;
echo "$silverpoundsgram pounds per gram of silver";
echo $br;
echo "$platpoundsgram pounds per gram of platinum";
echo $br;
echo $br;
// metal prices denominated in BTC
$btcgoldgram = ($goldpoundsgram / $btcgbpvwap);
$btcsilvergram = ($silverpoundsgram / $btcgbpvwap);
$btcplatgram = ($platpoundsgram / $btcgbpvwap);
echo "<b>";
echo $btcgoldgram;
echo " bitcoins per gram of gold";
echo $br;
echo $btcsilvergram;
echo " bitcoins per gram of silver";
echo $br;
echo $btcplatgram;
echo " bitcoins per gram of platinum";
?>