将标题和 curl 帖子发送到本地比特币

  • 本文关键字:标题 curl php api curl
  • 更新时间 :
  • 英文 :


我曾尝试在帖子中发送信息,并抛出localbitcoins标题并得到一个错误:

HMAC authentication key and signature was given, but they are invalid

从我了解到的localbitcoins api这只是一个代码,当你弄乱标题时,你会得到有人可以帮助我解决为什么我得到这个错误,因为不知道我的代码出了什么问题:

function localbitcoins_query2($path, array $req = Array()) {
    $key='mycode';
    $secret='mycode';
    $mt = explode(' ', microtime());
    $nonce = $mt[1].substr($mt[0], 2, 6);
    if ($req) {
        $get=httpbuildquery($req);
        $path=$path.'?'.$get;
    }
    $postdata=$nonce.$key.$path;
    $sign = strtoupper(hash_hmac('sha256', $postdata, $secret));
    $headers = array(
        'Apiauth-Signature:'.$sign,
        'Apiauth-Key:'.$key,
        'Apiauth-Nonce:'.$nonce
    );
    $ch = null;
    $ch = curl_init();
    $data = array("lat" => "Hagrid", "price_equation" => "36");                                                                    
    $data_string = json_encode($data);                                                                                                                                                                                            
    curl_setopt($ch, CURLOPT_URL,"https://localbitcoins.com".$path);                                                            
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                             
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);       
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
    $res = curl_exec($ch);
    if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
    $dec = json_decode($res, true);
    if (!$dec) throw new Exception('Invalid data: '.$res);
    curl_close($ch);
    return $dec;
}

试试这个解决方案:

function localbitcoins_query2($path, array $req = Array()) {
    $key='mycode';
    $secret='mycode';
    $mt = explode(' ', microtime());
    $nonce = $mt[1].substr($mt[0], 2, 6);
    $get = "";
    if ($req) {
        $get=httpbuildquery($req);
    }
    $postdata=$nonce.$key.$path.$get; // NOTE: $postdata 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();
    $data = array("lat" => "Hagrid", "price_equation" => "36");                                                                    
    $data_string = json_encode($data);                                                                                                                                                                                            
    curl_setopt($ch, CURLOPT_URL,"https://localbitcoins.com".$path.( $get=="" ? "" : "?".$get)); // NOTE:  here it's necesary '?' char before the parameters!);                                                            
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                             
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);       
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
    $res = curl_exec($ch);
    if ($res === false) throw new Exception('Curl error: '.curl_error($ch));
    $dec = json_decode($res, true);
    if (!$dec) throw new Exception('Invalid data: '.$res);
    curl_close($ch);
    return $dec;
}

最新更新