Google App Engine with Telegram Bot API through PHP 输出无空格

Google App Engine with Telegram Bot API through PHP output no whitespace

因此,我配置我的 PHP 脚本以将消息从我的联系表发送到我与机器人的电报聊天。

这是 PHP 脚本代码:

<?php
if(trim($_POST["gotcha"]) !== "") {
  header("Location: https://example.com/");
} else {
  if(isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["subject"]) && isset($_POST["content"])) {

    $wholeMessage = "Name:"." ".$_POST["name"]." "."|"." "."Email:"." ".$_POST["email"]." "."|"." "."Subject:"." ".$_POST["subject"]." "."|"." "."Message:"." ".$_POST["content"];
    $data = "https://api.telegram.org/bot<BOTID>/sendmessage?chat_id=<CHATID>&text=".$wholeMessage;

    $response = file_get_contents($data);

    header("Location: https://example.com/thanks/");
  } else {
    header("Location: https://example.com/");
  }
}
?>

我的 Telegram 聊天结果:

Name:firstname|Email:mail@mail.com|Subject:test|Message:testhttps://google.commouseappletest

仅使用%nbsp;输出:

Name:

没有其他输出。

如您所见,所有的空格都消失了。 我的代码有问题吗?

谢谢

您需要 URL Encode.

  • 使用 %20 作为空格。
  • 或者使用urlencode()函数来编码你的url。

这也可以使您的代码摆脱奇怪错误的危险,尤其是当您使用 url 包含中文或其他没有 url 编码的特殊字符时。