2017-12-02 279 views
0

我正在嘗試創建WordPress插件,該插件在PHP中爲我的網站交易cryptocoins,並且我試圖使用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'); 
} 
+1

什麼是例外? – valtron

+0

沒有特定的例外情況。唯一的輸出是「捕獲的異常:」,這是在引發異常時的輸出 –

+0

調試!去穴居人,並添加打印語句,以查找例外情況。 – zaph

回答

1

根據您的意見和異常APIKEY_NOT_PROVIDEDcall方法的一部分,默認情況下你的APIKEY是假的。

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

非常感謝您解決我的問題的幫助 –