Php 使用curl在电报bot中发送消息
我想从数据库中获取我的bot消息并发送给用户,当我的文本只有一个单词时,它就工作了,但现在它不工作,并显示HTTP错误请求400。我想我应该使用curl,但我没有发现任何文档对从电报机器人发送长消息有用。(实际上,因为我是php的初学者) 这是我的密码Php 使用curl在电报bot中发送消息,php,curl,telegram-bot,Php,Curl,Telegram Bot,我想从数据库中获取我的bot消息并发送给用户,当我的文本只有一个单词时,它就工作了,但现在它不工作,并显示HTTP错误请求400。我想我应该使用curl,但我没有发现任何文档对从电报机器人发送长消息有用。(实际上,因为我是php的初学者) 这是我的密码 $botToken="something"; $website="https://api.telegram.org/bot".$botToken; $update = file_get_contents("php://input"); $up
$botToken="something";
$website="https://api.telegram.org/bot".$botToken;
$update = file_get_contents("php://input");
$update = json_decode($update, true);
$chatID = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
define("dbhost", "localhost:3306"); define("dbuser", "sm");
define("dbpass", "sm"); define("database", "sm");
switch ($message){
case "/get":
connected($chatID);
break;
}
function sendMessage($chatID,$message){
$url =$GLOBALS[website]."/sendMessage?chat_id=".$chatID."&text=".$message;
//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);
//checkJSON($chatID,$update);
}
function connected($chatID){
$conn = mysql_connect(dbhost,dbuser,dbpass);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
mysql_select_db( database );
$strSQL = "SELECT text FROM bott WHERE id= '1' ";
$result = mysql_query($strSQL) or die('SQL Error :: '.mysql_error());
$row = mysql_fetch_assoc($result);
$message=$row['text'];
sendMessage($chatID,$message);
mysql_close($conn);
}
function checkJSON($chatID,$update){
$myFile = "log.txt";
$updateArray = print_r($update,TRUE);
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $chatID ."\n\n");
fwrite($fh, $updateArray."\n\n");
fclose($fh);
}
?>
这是我使用file\u get\u内容时出现的错误
[08-Jul-2016 12:27:35 America/Chicago] PHP Warning: file_get_contents(https://api.telegram.org/bot218048829:AAF7baKISagQfALp4OIPh- yU_hMOnpFGU0A/sendMessage? chat_id=98506693&text=Introduction%0D%0AA+COMPUTER+PROCESSOR+such+as+Intel%92s+i486+used+to+cost+around+the+same+as+a+small+car.+Nowadays+a+chip+with+similar+power+is+the+price+of+a+chocolate+bar.%0D%0A): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
in /home/sfduiir1/public_html/php.php on line 29
此代码不会向用户发送任何内容。在curl方法中,不应在url中使用任何数据。 正如您看到的那样,“获取错误”的url包含无效的空格。 这是一个工作旋度代码:
$website="https://api.telegram.org/bot".$botToken;
$chatId=1234567; //Receiver Chat Id
$params=[
'chat_id'=>$chatID,
'text'=>'MoonLight',
];
$ch = curl_init($website . '/sendMessage');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
是否启用了错误报告?是否显示任何错误?是的,这是此代码的报告,因为正在测试您的url并更改&;text=by&text=我收到了一个关于消息未进行utf8编码的400错误谢谢你@Edu,我使用urlencode(utf8_encode($message))将url更改为utf8好的,没错。电报以utf-8格式接收和发送所有信息。您好,先生,使用curl发送图像时,您有参考资料吗?我总是收到通知:无法打开流:HTTP请求失败!HTTP/1.1 404不是Found@IIMNURDIANSYAH对于你和其他有相同问题的人来说,最好是作为一个新问题来问。但根据你的要求,我写了一些示例代码。这是它的链接:我创建了一个新线程,但仍然没有得到答案,这是我的线程:@IIMNURDIANSYAH我在上面给你发了一些东西,希望对你有所帮助。