我对服务器(生产)中特定URL的所有POST类型请求都需要大约3分钟来返回响应,也就是说,在我提交表单几秒钟后,我查看了API站点并发布了我的数据,但在我的站点上请求是挂起的,恰好在3分钟后它就完成了必要数据的返回。
我想是因为我在本地环境中测试了相同的代码,并且它工作得很快(大约花了10秒),所以响应被抑制了3分钟,是什么导致了这种“缓慢”呢?
请看我的一小段代码,它在本地环境中工作得很好,但生产过程需要3分钟才能返回API结果。
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秒内返回响应。
发布于 2018-12-05 08:57:49
不确定参数中的时间戳和recvWindow是用来做什么的,但是如果您使用它们来检查特定的时间窗口,那么我会确保服务器配置了正确的时区。
https://stackoverflow.com/questions/53622691
复制相似问题