创建交易机器人异常
Exception creating a trading bot
我正在尝试创建 WordPress 插件来为我在 PHP 的网站交易加密货币,并且我正在尝试使用 Bittrex API 来达到这个目的。
我的问题是,当我尝试使用 API 从 Class 调用方法时,抛出了异常。
有人可以帮我找出代码中的问题吗?
这是主要 class 中的代码,我在其中从 Client
class.
创建一个对象
require 'bittrex-master/src/edsonmedina/bittrex/Client.php';
use edsonmedina\bittrex\Client;
$keya = "xxx";
$secreta = "xxx";
$b = new Client ($keya, $secreta);
try{
$list = $b->getMarkets ();
echo "$list";
}catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo "\n\n";
这是来自客户端的部分代码class
namespace edsonmedina\bittrex;
class Client
{
private $baseUrl;
private $apiVersion = 'v1.1';
private $apiKey;
private $apiSecret;
public function __construct ($apiKey, $apiSecret)
{
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->baseUrl = 'https://bittrex.com/api/'.$this->apiVersion.'/';
}
/**
* Invoke API
* @param string $method API method to call
* @param array $params parameters
* @param bool $apiKey use apikey or not
* @return object
*/
private function call ($method, $params = array(), $apiKey = false)
{
$uri = $this->baseUrl.$method;
if ($apiKey == true) {
$params['apikey'] = $this->apiKey;
$params['nonce'] = time();
}
if (!empty($params)) {
$uri .= '?'.http_build_query($params);
}
$sign = hash_hmac ('sha512', $uri, $this->apiSecret);
$ch = curl_init ($uri);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('apisign: '.$sign));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$answer = json_decode($result);
if ($answer->success == false) {
throw new \Exception ($answer->message);
}
return $answer->result;
}
/**
* Get the open and available trading markets at Bittrex along with other meta data.
* @return array
*/
public function getMarkets ()
{
return $this->call ('public/getmarkets');
}
根据您的评论和例外情况 APIKEY_NOT_PROVIDED
,默认情况下,您的 call
方法中的 APIKEY 为假。
return $this->call ('public/getmarkets', null, true);
我正在尝试创建 WordPress 插件来为我在 PHP 的网站交易加密货币,并且我正在尝试使用 Bittrex API 来达到这个目的。
我的问题是,当我尝试使用 API 从 Class 调用方法时,抛出了异常。 有人可以帮我找出代码中的问题吗?
这是主要 class 中的代码,我在其中从 Client
class.
require 'bittrex-master/src/edsonmedina/bittrex/Client.php';
use edsonmedina\bittrex\Client;
$keya = "xxx";
$secreta = "xxx";
$b = new Client ($keya, $secreta);
try{
$list = $b->getMarkets ();
echo "$list";
}catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo "\n\n";
这是来自客户端的部分代码class
namespace edsonmedina\bittrex;
class Client
{
private $baseUrl;
private $apiVersion = 'v1.1';
private $apiKey;
private $apiSecret;
public function __construct ($apiKey, $apiSecret)
{
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->baseUrl = 'https://bittrex.com/api/'.$this->apiVersion.'/';
}
/**
* Invoke API
* @param string $method API method to call
* @param array $params parameters
* @param bool $apiKey use apikey or not
* @return object
*/
private function call ($method, $params = array(), $apiKey = false)
{
$uri = $this->baseUrl.$method;
if ($apiKey == true) {
$params['apikey'] = $this->apiKey;
$params['nonce'] = time();
}
if (!empty($params)) {
$uri .= '?'.http_build_query($params);
}
$sign = hash_hmac ('sha512', $uri, $this->apiSecret);
$ch = curl_init ($uri);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('apisign: '.$sign));
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$answer = json_decode($result);
if ($answer->success == false) {
throw new \Exception ($answer->message);
}
return $answer->result;
}
/**
* Get the open and available trading markets at Bittrex along with other meta data.
* @return array
*/
public function getMarkets ()
{
return $this->call ('public/getmarkets');
}
根据您的评论和例外情况 APIKEY_NOT_PROVIDED
,默认情况下,您的 call
方法中的 APIKEY 为假。
return $this->call ('public/getmarkets', null, true);