简体   繁体   English

电报机器人从服务器发送消息失败(PHP)

[英]telegram bot send message from server failed (PHP)

I need a simple bot to send message via telegram. 我需要一个简单的机器人来通过电报发送消息。 I use this php code for the reason: 我使用此php代码的原因是:

header('Content-Type: text/html; charset=utf-8');
$message= file_get_contents("php://input");
$arrayMessage= json_decode($message, true);
$token= "*********";
$chat_id= $arrayMessage['message']['from']['id'];
$command= $arrayMessage['message']['text'];
    if($command == '/start'){
      $text= "Hello";
      $url= "https://api.telegram.org/bot".$token."/sendMessage?chat_id=".$chat_id."&text=".$text;
      file_put_contents("message.txt", $url);
      //file_get_contents ($url);
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      $output = curl_exec($ch);   
      curl_close($ch);
      }
  1. When i use file_get_contents i receive http bad request 400 .... 当我使用file_get_contents时,收到HTTP错误请求400...。
  2. With curl nothing changed 卷曲没有改变
  3. I put the $url in text file and i used it in the browser and it works great 我将$ url放在文本文件中,并在浏览器中使用了它,效果很好

any suggestion? 有什么建议吗?

i use cloudflare for ssl certificate and the webhook method is initiated. 我将cloudflare用于ssl证书,并启动了webhook方法。

Maybe you need to add 也许您需要添加

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

and other debugging code to find the error as follows: 和其他调试代码查找错误如下:

var_dump ( curl_getinfo ( $ch ) );
if (curl_errno ( $ch )) {
    print curl_error ( $ch );
} else {
    curl_close ( $ch );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM