doudang4857 2016-08-01 14:53
浏览 28

加快PHP中的Telegram Bot API

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
    • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
    • ¥30 关于<main>标签页面跳转的问题
    • ¥80 部署运行web自动化项目
    • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
    • ¥30 VMware 云桌面水印如何添加
    • ¥15 用ns3仿真出5G核心网网元