Bitstamp API的PHP实现
我试图通过将PHP代码放在crontab中来实现一个PHP API到bitstamp,以执行一个重复发生的事务 我试图让API与bitstamp通信,以便每次执行购买X数量的BTC(然后从crontab控制频率),这应该是基本实现的定义 这就是乐趣,我绝对不是一个PHP程序员。BX媒体公司的人很好,在github上发布了他们的PHP框架:() 然而,我理解他们所做的事情的方式是,我必须创建“父”PHP,然后包括他们的API代码,其中也包括我的凭证 所以我把最基本的PHP代码放在一起Bitstamp API的PHP实现,php,api,bitcoin,Php,Api,Bitcoin,我试图通过将PHP代码放在crontab中来实现一个PHP API到bitstamp,以执行一个重复发生的事务 我试图让API与bitstamp通信,以便每次执行购买X数量的BTC(然后从crontab控制频率),这应该是基本实现的定义 这就是乐趣,我绝对不是一个PHP程序员。BX媒体公司的人很好,在github上发布了他们的PHP框架:() 然而,我理解他们所做的事情的方式是,我必须创建“父”PHP,然后包括他们的API代码,其中也包括我的凭证 所以我把最基本的PHP代码放在一起 <?p
<?php
require('bitstamp.php');
$KEY = 'XXXXXXXXXXXXXXXX';
$SECRET = 'XXXXXXXXXXXXXXXX';
$CLIENT_ID = 'XXXXX';
$bs = new Bitstamp("KEY","SECRET","CLIENT_ID");
// print_r($bs->ticker());
$bs->buyBTC(0.01); // buy 0.01 bitcoins at ask price
//$bs->bitstamp_query("buy", array('amount'=>'0.05','price'=>'50'));
?>
注意:“bitstamp.php”在同一个目录中,目前在Github上
<?php
/**
* @package Bitstamp API
* @author https://bxmediaus.com - BX MEDIA - PHP + Bitcoin. We are ready to work on your next bitcoin project. Only high quality coding. https://bxmediaus.com
* @version 0.1
* @access public
* @license http://www.opensource.org/licenses/LGPL-3.0
*/
class Bitstamp
{
private $key;
private $secret;
private $client_id;
public $redeemd; // Redeemed code information
public $withdrew; // Withdrawal information
public $info; // Result from getInfo()
public $ticker; // Current ticker (getTicker())
public $eurusd; // Current eur/usd
/**
* Bitstamp::__construct()
* Sets required key and secret to allow the script to function
* @param Bitstamp API Key $key
* @param Bitstamp Secret $secret
* @return
*/
public function __construct($key, $secret, $client_id)
{
if (isset($secret) && isset($key) && isset($client_id))
{
$this->key = $key;
$this->secret = $secret;
$this->client_id = $client_id;
} else
die("NO KEY/SECRET/CLIENT ID");
}
/**
* Bitstamp::bitstamp_query()
*
* @param API Path $path
* @param POST Data $req
* @return Array containing data returned from the API path
*/
public function bitstamp_query($path, array $req = array())
{
// API settings
$key = $this->key;
// generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
$mt = explode(' ', microtime());
$req['nonce'] = $mt[1] . substr($mt[0], 2, 6);
$req['key'] = $key;
$req['signature'] = $this->get_signature($req['nonce']);
// generate the POST data string
$post_data = http_build_query($req, '', '&');
// any extra headers
$headers = array();
// our curl handle (initialize if required)
static $ch = null;
if (is_null($ch))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT,
'Mozilla/4.0 (compatible; MtGox PHP client; ' . php_uname('s') . '; PHP/' .
phpversion() . ')');
}
curl_setopt($ch, CURLOPT_URL, 'https://www.bitstamp.net/api/' . $path .'/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// run the query
$res = curl_exec($ch);
if ($res === false)
throw new \Exception('Could not get reply: ' . curl_error($ch));
$dec = json_decode($res, true);
if (!$dec)
throw new \Exception('Invalid data received, please make sure connection is working and requested API exists');
return $dec;
}
/**
* Bitstamp::ticker()
* Returns current ticker from Bitstamp
* @return $ticker
*/
function ticker() {
$ticker = $this->bitstamp_query('ticker');
$this->ticker = $ticker; // Another variable to contain it.
return $ticker;
}
/**
* Bitstamp::eurusd()
* Returns current EUR/USD rate from Bitstamp
* @return $ticker
*/
function eurusd() {
$eurusd = $this->bitstamp_query('eur_usd');
$this->eurusd = $eurusd; // Another variable to contain it.
return $eurusd;
}
/**
* Bitstamp::buyBTC()
*
* @param float $amount
*/
function buyBTC($amount){
if (!isset($ticker))
$this->ticker();
$ticker = $this->ticker;
return $this->bitstamp_query('buy', array('amount' => $amount, 'price' => $ticker['ask']));
}
/**
* Bitstamp::sellBTC()
*
* @param float $amount
* @param float $price
* @param string $currency
*/
function sellBTC($amount){
if (!isset($ticker))
$this->ticker();
$ticker = $this->ticker;
return $this->bitstamp_query('sell', array('amount' => $amount, 'price' => $ticker['bid']));
}
/**
* Bitstamp::get_signature()
* Compute bitstamp signature
* @param float $nonce
*/
private function get_signature($nonce)
{
$message = $nonce.$this->client_id.$this->key;
return strtoupper(hash_hmac('sha256', $message, $this->secret));
}
}
key=$key;
$this->secret=$secret;
$this->client\u id=$client\u id;
}否则
die(“无密钥/机密/客户ID”);
}
/**
*Bitstamp::Bitstamp_查询()
*
*@param API Path$Path
*@param POST Data$req
*@return数组,包含从API路径返回的数据
*/
公共函数bitstamp_查询($path,array$req=array())
{
//API设置
$key=$this->key;
//生成一个nonce作为microtime,并进行字符串处理,以避免32位系统出现问题
$mt=爆炸(“”,microtime());
$req['nonce']=$mt[1]。substr($mt[0],2,6);
$req['key']=$key;
$req['signature']=$this->get_signature($req['nonce']);
//生成POST数据字符串
$post_data=http_build_query($req,,'&');
//有额外的标题吗
$headers=array();
//我们的卷曲句柄(如果需要初始化)
静态$ch=null;
如果(为空($ch))
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,
'Mozilla/4.0(兼容;MtGox PHP客户端;'.PHP_uname('s');PHP/'。
phpversion();
}
curl_setopt($ch,CURLOPT_URL,'https://www.bitstamp.net/api/“.$path./”);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
//运行查询
$res=curl_exec($ch);
如果($res==false)
抛出new\Exception('无法获得回复:'。curl_错误($ch));
$dec=json_decode($res,true);
如果(!$dec)
抛出new\Exception('接收到无效数据,请确保连接正在工作并且请求的API存在');
返回$dec;
}
/**
*Bitstamp::ticker()
*从Bitstamp返回当前股票代码
*@return$ticker
*/
函数代码(){
$ticker=$this->bitstamp_查询('ticker');
$this->ticker=$ticker;//包含它的另一个变量。
返回$ticker;
}
/**
*比特邮票::欧元兑美元()
*从Bitstamp返回当前欧元/美元汇率
*@return$ticker
*/
功能欧元兑美元(){
$eurusd=$this->bitstamp_查询('eur_usd');
$this->eurusd=$eurusd;//包含它的另一个变量。
返回$EURMUSD;
}
/**
*Bitstamp::buyBTC()
*
*@param float$金额
*/
函数buyBTC($amount){
如果(!isset($ticker))
$this->ticker();
$ticker=$this->ticker;
返回$this->bitstamp_查询('buy',array('amount'=>$amount,'price'=>$ticker['ask']);
}
/**
*比特邮票::sellBTC()
*
*@param float$金额
*@param浮动$price
*@param字符串$currency
*/
函数sellBTC(金额){
如果(!isset($ticker))
$this->ticker();
$ticker=$this->ticker;
返回$this->bitstamp_查询('sell',array('amount'=>$amount,'price'=>$ticker['bid']);
}
/**
*Bitstamp::get_签名()
*计算位戳签名
*@param float$nonce
*/
私有函数get_签名($nonce)
{
$message=$nonce.$this->client_id.$this->key;
返回strtoupper(hash_hmac('sha256',$message,$this->secret));
}
}
我执行死刑失败了。由于Bitstamp API的作者显然与他的客户机一起工作,我假设错误出现在我的“父”PHP代码上。(注意:我在本地版本中使用的是真正的密钥和机密)
任何人都有使用此API的经验或一般建议吗?我不确定这是匿名还是实际代码,因此如果我有错误,请告诉我,但您有以下几行:
$bs = new Bitstamp("KEY","SECRET","CLIENT_ID");
这将实际字符串“KEY”、“SECRET”和“CLIENT_ID”传递给函数;您要做的是传递您在上面几行中定义的变量,如下所示:
$bs = new Bitstamp($KEY,$SECRET,$CLIENT_ID);
$bs = new Bitstamp("put your key here","put your secret here","put your client ID here");
作为本api的作者(我是Bx Media的首席执行官):
您需要像这样重新生成构造函数:
$bs = new Bitstamp($KEY,$SECRET,$CLIENT_ID);
$bs = new Bitstamp("put your key here","put your secret here","put your client ID here");
你实际上把你的钥匙/秘密/身份证放在了语音标记之间
在PHP中,当您输入$符号时,它会将某些内容设置为变量
请参见此处的更多信息:
如果您想使用变量,还可以使用IMSoP的解决方案
请注意,我们今晚还更新了库的一些新功能。因此,请将您的回购更新为最新提交,以获得新代码
干杯 它不会抛出错误。。它只是没有完成请求的事务。我假设我没有正确编码“父”php以将变量(例如登录凭据)传递给bitstamp.php代码。