首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >对特定URL的POST请求需要3分钟

对特定URL的POST请求需要3分钟
EN

Stack Overflow用户
提问于 2018-12-05 06:57:05
回答 1查看 179关注 0票数 0

我对服务器(生产)中特定URL的所有POST类型请求都需要大约3分钟来返回响应,也就是说,在我提交表单几秒钟后,我查看了API站点并发布了我的数据,但在我的站点上请求是挂起的,恰好在3分钟后它就完成了必要数据的返回。

我想是因为我在本地环境中测试了相同的代码,并且它工作得很快(大约花了10秒),所以响应被抑制了3分钟,是什么导致了这种“缓慢”呢?

请看我的一小段代码,它在本地环境中工作得很好,但生产过程需要3分钟才能返回API结果。

代码语言:javascript
复制
public function __construct()
{

    $this->url = 'https://api.binance.com/api/';
    $this->curl = curl_init();
    $this->recvWindow = 60000;

    $curl_options = [
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_USERAGENT => 'Binance PHP API Agent',
        CURLOPT_RETURNTRANSFER => true,

    ];

    curl_setopt_array($this->curl, $curl_options);
}

private function privateRequest($url, $params = [], $method = 'GET')
{
    $params['timestamp'] = number_format((microtime(true) * 1000), 0, '.', '');
    $params['recvWindow'] = $this->recvWindow;

    $query = http_build_query($params, '', '&');

    $sign = hash_hmac('sha256', $query, $this->secret);

    $headers = array(
        'X-MBX-APIKEY: ' . $this->key,
    );

    curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($this->curl, CURLOPT_URL, $this->url . $url . "?{$query}&signature={$sign}");

    curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($this->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($this->curl, CURLOPT_ENCODING, '');

    if ($method == "POST") {
        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, array());
    }

    if($method == 'GET'){
        curl_setopt($this->curl, CURLOPT_POST, false);
    }

    if ($method == 'DELETE') {
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
    }


    //Get result
    $result = curl_exec($this->curl);

    if ($result === false) {
        throw new \Exception('CURL error: ' . curl_error($this->curl));
    }

    // Decode results
    $result = json_decode($result, true);
    if (!is_array($result) || json_last_error()) {
        throw new \Exception('JSON decode Error');
    }

    return $result;
}

curl不会产生任何错误,因为API返回状态200以及所需的数据,问题是返回响应需要3分钟。

注意:在其他API中,post在生产环境中工作得很好,最多在5秒内返回响应。

EN

回答 1

Stack Overflow用户

发布于 2018-12-05 08:57:49

不确定参数中的时间戳和recvWindow是用来做什么的,但是如果您使用它们来检查特定的时间窗口,那么我会确保服务器配置了正确的时区。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53622691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档