发送标题并以curl发布到localbitcoins

时间:2017-01-24 03:44:24

标签: 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;
}

1 个答案:

答案 0 :(得分:0)

尝试以下解决方案:

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;
}