tanto259 tanto259 - 25 days ago 8
PHP Question

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

So, I configure my PHP script to send message from my contact form to my telegram chat with a bot.

Here's the PHP script code:

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/");

The result on my Telegram chat:


only output:


with nothing else outputted.

As you can see, all the whitespace has disappeared.
Is there something wrong with my code?



You need a URL Encode.

  • Use %20 as whitespace.
  • Or use urlencode() function to encode your url.

This can also rid your code of the danger of strange errors, especially when you are using a url containing Chinese or other special characters without url encode.