创建交易机器人的例外情况

时间:2017-12-02 19:55:39

标签: php wordpress curl exception trading

我正在尝试创建WordPress插件,在PHP中为我的网站交换cryptocoins,我正在尝试使用Bittrex API。

我的问题是,当我尝试使用API​​调用Class中的方法时,会抛出异常。 有人可以帮助我在我的代码中找到问题吗?

这是主类中的代码,我在Client类创建一个Object。

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";

以下是Client类的代码部分

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');
}

1 个答案:

答案 0 :(得分:1)

根据您的评论和例外APIKEY_NOT_PROVIDED,默认情况下,您的call方法中的APIKEY为false。

return $this->call ('public/getmarkets', null, true);