I have used PHP in order to write a Telegram bot that uses webhook
which means that the PHP file is called for each update. I have used curl in order to post JSON to https://api.telegram.org. When I send a message in my PHP file, it responds in around 1 second. I think because it does not reuse the curl connection. the connection closes after the PHP file finishes.
I have also written a Java application that calls getUpdate
method every 5 seconds. It responds in around 500 milliseconds because it reuses the connection.
How can I speed up my PHP bot just like my Java application? Is there any way to reuse the connection even when the PHP file is finished and another PHP file is run?
Here is my PHP code:
function Post($method, $data)
{
$url = "https://api.telegram.org/bot".$authenticationToken."/".$method;
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_ENCODING, '');
$resultJSON = curl_exec($curl);
curl_close($curl);
$result = json_decode($resultJSON,true);
if(!$result["ok"])
LogF($resultJSON);
return $result;
}
P.S: the ping time of the URL is around 200 milliseconds.