【问题标题】:Exception creating a trading bot创建交易机器人的异常
【发布时间】:2018-05-16 15:41:27
【问题描述】:

我正在尝试创建 WordPress 插件,该插件可以用 PHP 为我的网站交易加密币,并且我正在尝试为此目的使用 Bittrex API。

我的问题是,当我尝试使用 API 从类中调用方法时,会引发异常。 有人可以帮我找出代码中的问题吗?

这是主类中的代码,我在其中从Client 类创建一个对象。

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

这是来自客户端类的部分代码

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

【问题讨论】:

  • 有什么异常?
  • 没有特定的例外。唯一的输出是“Caught exception:”,这是抛出异常时的输出
  • 调试!去穴居人并添加打印语句以查找异常在哪里。
  • 我收到异常 APIKEY_NOT_PROVIDED

标签: php wordpress curl exception trading


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您帮助它解决了我的问题
猜你喜欢
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 2017-04-20
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
相关资源
最近更新 更多