2017-02-02 3 views
0

PHP 샘플 코드를 파이썬으로 변경하면 심각한 문제가 발생합니다. Bitmarket.pl API를 PHP에서 Python으로 번역

function bitmarket_api($method, $params = array()) 

{ 
    $key = "klucz_jawny"; 
    $secret = "klucz_tajny"; 

    $params["method"] = $method; 
    $params["tonce"] = time(); 

    $post = http_build_query($params, "", "&"); 
    $sign = hash_hmac("sha512", $post, $secret); 
    $headers = array(
     "API-Key: " . $key, 
     "API-Hash: " . $sign, 
    ); 

    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_URL, "https://www.bitmarket.pl/api2/"); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
    $ret = curl_exec($curl); 

    return json_decode($ret); 
} 

가 사전에 어떤 도움을 주셔서 감사합니다 : 다음은 예제 PHP 코드입니다.

UPDATE :

내 코드는 다음과 같습니다

apiurl = "https://www.bitmarket.pl/api2/" 
key = "mypubkey" 
secret = "myceretkey" 

apicommand = "info" 
tonce = time.time() 

params = str(apicommand) + " " + str(tonce) 

postdata = (params + " " + "&") 

signdata = hmac.new(postdata, secret, hashlib.sha512).hexdigest() 

headerapi = { "API-Key: ": key, 
"API-Hash: " : signdata} 



getapi = requests.post(apiurl, data=headerapi ,params=postdata) 
print getapi.text 

결과 : { "오류"501 "에서 errormsg": "잘못된 API 키", "시간": 1486049060}을

+0

어제 올바른 요청을 보내려고 몇 시간을 보내고 파이썬 요청 및 urllib2를 통해 지금은 전혀 작동하지 않았기 때문에 전체 코드를 삭제했습니다. 모든 요청이 잘못된 인증 메시지로 완료되었습니다. 누군가가 데이터를 어떻게 게시해야하는지 알려주는 경우. 나는 PHP를 모른다. 그래서 그것을 알아내는 것이 어렵다. – MichalM

+0

Google을 사용해 보셨습니까? "파이썬으로 데이터 게시"하시겠습니까? –

+0

네, 저는 메신저가 잘못된 데이터를 보냈다 고 생각합니다. apiurl = "https://www.bitmarket.pl/api2/" apidata = hmac.new ("info", secret_key_here, hashlib.sha512) .hexdigest() getapi = requests.post (apiurl, data = apidata) – MichalM

답변

0

당신은 다른 요구를 정확히 내가 지적

Curl issue in subprocess Python

에서보세요 누군가가 (.이가 당신에게 방법의 약 90 %를 얻어야한다) 당신을 위해 파이썬 PHP를 번역하는 다음에 대한

1

을 솔루션입니다 : 내가했습니다

def mergeTwoDicts(x, y): 
    z = x.copy() 
    z.update(y) 
    return z 


def bitMarketPlApiCall(method, params = {}): 
    postDataAsDict = mergeTwoDicts(params, { 
     'method': method, 
     'tonce': int(time.time()) 
    }) 
    postParamsAsString = "&".join([param + '=' +  str(postDataAsDict[param]) for param in postDataAsDict]) 

    postHeaders = { 
     'API-Key': publicKey, 
     'API-Hash': hmac.new(secretKey, postParamsAsString, hashlib.sha512).hexdigest() 
} 

    request_response = requests.post('https://www.bitmarket.pl/api2/', data = postParamsAsString, headers = postHeaders) 

    return request_response.text