gpt4 book ai didi

PHP vs Golang http 调用得到不同的结果

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:34 27 4
gpt4 key购买 nike

我正在尝试在 Google App Engine Go 中实现以下 PHP 代码:

<?php

function api_query(array $req = array()) {
$key = '90294318da0162b082c3d27126be80c3873955f9';

$req['method'] = 'getinfo';
$req['nonce'] = 1394503747386411;

// generate the POST data string
$post_data = http_build_query($req, '', '&');
$sign = '75da1e3ff750286bf73d03197f1b779fbfff963fd7402941ae326509a6615eacb839b44f236b4d5ee6cff39321e7b35e9563a9a2075e99df0f4ee3b732999348';

// generate the extra headers
$headers = array(
'Sign: '.$sign,
'Key: '.$key,
);

// our curl handle (initialize if required)
static $ch = null;
if (is_null($ch)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Cryptsy API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, 'https://api.cryptsy.com/api');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

// run the query
$res = curl_exec($ch);

if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');

echo "<pre>".print_r($dec, true)."</pre>";
return $dec;
}

api_query();

执行时,代码返回一个 JSON 值数组。我尝试在 Golang 中实现相同的代码:

func PrivateCall(c appengine.Context) (map[string]interface{}, error) {
AuthAPI := "https://api.cryptsy.com/api"
APIKey := "90294318da0162b082c3d27126be80c3873955f9"
tr := urlfetch.Transport{Context: c}
values := url.Values{}
values.Set("method", "getinfo")
values.Set("nonce", "1394503747386411")

signature := "75da1e3ff750286bf73d03197f1b779fbfff963fd7402941ae326509a6615eacb839b44f236b4d5ee6cff39321e7b35e9563a9a2075e99df0f4ee3b732999348"

req, err := http.NewRequest("POST", AuthAPI+"?"+values.Encode(), nil)
if err != nil {
c.Infof("API - Call - error 2 - %s", err.Error())
return nil, err
}
req.Header.Set("Key", APIKey)
req.Header.Set("Sign", signature)

c.Infof("req - %v", req)
resp, err := tr.RoundTrip(req)
if err != nil {
c.Errorf("API post error: %s", err)
return nil, err
}
defer resp.Body.Close()
//reading response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.Errorf("API read error: could not read body: %s", err)
return nil, err
}
result := make(map[string]interface{})
//unmarshalling JSON response
err = json.Unmarshal(body, &result)
if err != nil {
c.Infof("Unmarshal: %v", err)
c.Infof("%s", body)
return nil, err
}
return result, nil
}

我收到一条错误消息“无法授权请求 - 检查您的发布数据”。有没有人看到可能导致此错误的原因?目前我最好的猜测是 Go 中的请求 header 可能是一个 map[string][]string,而在 PHP 中它似乎是一个数组...

最佳答案

按照 LeGEC 的建议,您将 POST 数据放在 URL 的末尾,就好像它是一个 GET 请求一样。

尝试替换

values := url.Values{}
values.Set("method", "getinfo")
values.Set("nonce", "1394503747386411")

signature := "75da1e3ff750286bf73d03197f1b779fbfff963fd7402941ae326509a6615eacb839b44f236b4d5ee6cff39321e7b35e9563a9a2075e99df0f4ee3b732999348"

req, err := http.NewRequest("POST", AuthAPI+"?"+values.Encode(), nil)

data := struct {
method string
nonce string
}{
"getinfo",
"1394503747386411",
}
signature := "75da1e3ff750286bf73d03197f1b779fbfff963fd7402941ae326509a6615eacb839b44f236b4d5ee6cff39321e7b35e9563a9a2075e99df0f4ee3b732999348"
postData, err := json.Marshal(data)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(postData)
req, err := http.Post(AuthAPI, "application/json", buf)

关于PHP vs Golang http 调用得到不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22315378/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com