All my POST-type requests for a specific URL in the server (production) are taking about 3 minutes to return the response, that is, a few seconds after I submit the form, I look at the API site and there are my data posted, but on my site the request is pending, after exactly 3 minutes it is finished returning the necessary data.
I think something is holding the response for 3 minutes because I tested the same code in the local environment and it worked quickly (it took about 10 seconds), what could be causing this "slowness"?
Look at a short snippet of my code, which works fine in the local environment, but it takes 3 minutes for production to return the API result.
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;
}
The curl does not generate any error because the API returns status 200 with the required data, the problem is that it takes 3 minutes to return the response.
Note: In other APIs the post works very well in production, returning the response in a maximum of 5 seconds.
Not sure what the timestamp and recvWindow in the params are for, but if you're using them to check for specific time windows, then I would make sure the server is configured with the correct timezone.