PHP - 从API获取结果(CURL)

时间:2017-10-13 17:35:36

标签: php python api curl

我正在尝试从API返回结果,但它给了我一个身份验证失败的消息,而不是我需要的结果。

我正在使用novaexchange API,它在此页面上提供了一个Python示例:

https://novaexchange.com/remote/faq/

这是我移植到PHP的当前代码:

function get_private_data($method) {
    $apikey = 'key_here';
    $apisecret = 'secret_here'; // signature
    $nonce = time();
    $uri = 'https://novaexchange.com/remote/v2/private/' . $method . '/?nonce=' . $nonce;
    $sign = hash_hmac('sha512',$uri,$apisecret);
    $ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('content-type: application/x-www-form-urlencoded', 'apikey: ' . $apikey, 'signature: ' . $sign));
    $execResult = curl_exec($ch);
    $obj = json_decode($execResult);
    return $obj;
    curl_close($ch);
}

echo get_private_data('getbalances'); // BROKEN

如何让代码正常工作?

编辑:尝试使用Post我也会收到错误:

function get_private_data($method) {
    $apikey = 'key_here';
    $apisecret = 'secret_here'; // signature
    $nonce = time();
    $uri = 'https://novaexchange.com/remote/v2/private/' . $method . '/?nonce=' . $nonce;
    $sign = hash_hmac('sha512',$uri,$apisecret);

    $fields = array(
        'content-type' => 'application/x-www-form-urlencoded',
        'apikey' => $apikey,
        'signature' => $sign
    );

    $ch = curl_init($uri);

    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $uri);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $execResult = curl_exec($ch);
    //$obj = json_decode($execResult);
    //return $obj;
    return $execResult;
    curl_close($ch);
}

0 个答案:

没有答案