2016-06-24 4 views
-1

내가 그 오류 코드지고있어 (내가 확실히 그들의 편에서 일하고 있도록 공개 API를 사용하고 있습니다를)) :PHP - HMAC 인증

HMAC 인증 키와 서명이 주어졌다, 그러나 그들은이다 유효하지 않습니다.

function get_myself($request){ 
    $public_key = "MY_PUBLIC_KEY"; 
    $secret = "MY_PRIVATE_KEY"; 

    $parameters = array(
     "client_id" => $public_key, 
     "client_secret" => $secret 
    ); 
    $data = http_build_query($parameters); 

    $ch = curl_init("https://localbitcoins.com".$request); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_USERAGENT, "curl"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    $nonce = time(); 
    $sig = base64_encode (hash_hmac("sha256", $nonce.$public_key.$request, $secret)); 
    $options = array(
     CURLOPT_RETURNTRANSFER => 1, 
     CURLOPT_TIMEOUT => 30, 
     CURLOPT_HTTPHEADER => array(
      "Apiauth-Key:".$public_key, 
      "Apiauth-Nonce:".$nonce, 
      "Apiauth-Signature:".$sig 
     ), 
    ); 
    curl_setopt_array($ch, $options); 
    $result = curl_exec($ch); 
    curl_close($ch); 
    return $result; 
} 

$getinfo = array(); 
$getinfo = get_myself("/api/myself/"); 
echo "<pre>"; print_r($getinfo); echo "</pre>"; 
+0

가 https://localbitcoins.com/api-docs/errors/ 당신이해야한다고 말한다 "키, 비밀 및 서명 계산의 유효성을 확인합니다." – moxn

+1

나는 자격증을 가지고 있는데 문제가 어디서 오는지는 알 수 없다 ... 다른 사람이 그 문제를 가지고있는 경우를 대비하여 온라인에서 다른 코드가 작동 중임을 알게되었다 ... 나는 그것을 게시 할 것이다;) –

답변

1

3 일 후, 나는 '솔루션'... 여기에 작업 예제 발견

function localbitcoins_query($path, array $req = Array()) { 
    $key='MY_KEY'; 
    $secret='MY_SECRET'; 
    $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('https://localbitcoins.com'.$path); 
    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(); 
$devise = "EUR"; 
$url = "/buy-bitcoins-online/".$devise."/western-union/.json"; 

$getinfo = localbitcoins_query($url); 
echo "<pre>"; print_r($getinfo); echo "</pre>"; 

그것은 내 옆에 일하고, 나는 POST를 가정을/GET 개념은 아니었다 이전 버전에서는 적절하게 다루지 않았습니다.

는 즐기십시오 : P

+0

너 자신의 대답을 승인 할 수 있다는 것을 잊어라. –

관련 문제