How to use pagination at api localbitcoin

丶灬走出姿态 提交于 2019-12-23 03:32:48

问题


I'm developing with localbitcoin API and i am using path “/api/dashboard/closed/” and this is my code:

<?php
function localbitcoinsquery($path, $nonce,array $req = Array()) {
    global $random;
    $key='mykey';
    $secret='secretkey';

    if ($req) {
        $get=httpbuildquery($req);
        $path=$path.'?'.$get;
    }

    $postdata=$nonce.$key.$path;
    $sign = strtoupper(hashhmac('sha256', $postdata, $secret));
    $headers = array(
        'Apiauth-Signature:'.$sign,
        'Apiauth-Key:'.$key,
        'Apiauth-Nonce:'.$nonce
    );

    $ch = null;
    $ch = curlinit('https://localbitcoins.com'.$path);
    curlsetopt($ch, CURLOPTRETURNTRANSFER, true);
    curlsetopt($ch, CURLOPTHTTPHEADER, $headers);
    curlsetopt($ch, CURLOPTSSLVERIFYPEER, TRUE);
    curlsetopt($ch, CURLOPTCONNECTTIMEOUT, 20);
    $res = curlexec($ch);
    if ($res === false) throw new Exception('Curl error: '.curlerror($ch));
    $dec = jsondecode($res, true);
    if (!$dec) throw new Exception('Invalid data: '.$res);
    curl_close($ch);

    return $dec;
}

$getinfo = array();
$url='/api/dashboard/closed/';
$mt = explode(' ', microtime());
$random = $mt[1].substr($mt[0], 2, 6);
$getinfo = localbitcoinsquery($url,$random);
echo "<pre>";
printr($getinfo); 
echo "</pre>";
?>

This works OK, but show only 50 trades, Also I get this at result:

[pagination] => Array
(
[next] => https://localbitcoins.com/api/dashboard/closed/?order_by=-closed_at&start_at=2017-10-26+17%3U50%3A49%2B00%9A00
)

But I don't know how to use pagination, when I try to use this link at my code I get error:

[message] => HMAC authentication key and signature was given, but they are invalid. Error 41

I already investigated at google large time but the information is scarce.


回答1:


I'm using the python library and had the same issue. When I spoke to technical support they said the issue was in the way I was calculating the authentication.

Basically you have to include the pagination url as part of the signature.

On the python library at least, you do not have to change the api endpoint since arguments are being delivered as part of the form data.

So you still access for example "/api/dashboard/closed/" when getting the second page and the "?order_by=-closed_at&start_at=2017-10-26+17%3U50%3A49%2B00%9A00" stuff goes in the form somehow.

The python API does all this for you, you just have to copy the example from the github page.




回答2:


I fixed error no. 41. I modified your example to show that works, (read my NOTE: comments to understand better where is the problem) Read my NOTE: comments.

<?php
function localbitcoins_query($path, array $req = Array()) { 
   $key='yourkey';
   $secret='yoursecret';      

   $array_mt = explode(' ', microtime());   
   $nonce = $array_mt[1].substr($array_mt[0], 2, 6);   

   $get = "";
   if ($req) {
      $get=http_build_query($req);
   }
   $postdata=$nonce.$key.$path.$get; // NOTE: here $postdata goes without '?' char before the parameters!

   $sign = strtoupper(hash_hmac('sha256', $postdata, $secret)); 

   $headers = array(
      'Apiauth-Signature:'.$sign,
      'Apiauth-Key:'.$key,
      'Apiauth-Nonce:'.$nonce
   );
   $ch = null;
   $ch = curl_init('https://localbitcoins.com'.$path.( $get=="" ? "" : "?".$get)); // NOTE:  here it's necesary '?' char before the parameters!
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);

   $res = curl_exec($ch);
   if ($res === false) throw new Exception('Curl error: '.curlerror($ch));
   $dec = json_decode($res, true);   
   if (!$dec) throw new Exception('Invalid data: '.$res);
   curl_close($ch);   
   return $dec;
}

$getinfo = array();
$api_endpoint = '/api/dashboard/closed/';
$array_params = array(    "order_by" => "-closed_at"
                        , "start_at" => "2019-08-14 18:00:26+00:00" 
                        );
$getinfo = localbitcoins_query($api_endpoint,$array_params);
echo "<pre>"; print_r($getinfo); echo "</pre>"; 
?


来源:https://stackoverflow.com/questions/47046641/how-to-use-pagination-at-api-localbitcoin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!