如何在api localbitcoin上使用分页 [英] How to use pagination at api localbitcoin

查看:117
本文介绍了如何在api localbitcoin上使用分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用localbitcoin API进行开发,并且正在使用路径"/api/dashboard/closed/",这是我的代码:

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

这可以正常运行,但仅显示50笔交易, 我也得到这个结果:

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:

[消息] =>给出了HMAC身份验证密钥和签名,但是它们 是无效的.错误41

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

我已经在Google进行了大量调查,但相关信息很少.

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

推荐答案

我更正了错误编号. 41.我修改了您的示例以显示其效果,(请阅读我的NOTE:评论以更好地了解问题所在).阅读我的NOTE:评论.

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

这篇关于如何在api localbitcoin上使用分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆