0

I'm triying to do a BOT for telegram using PHP code.

Basically the user have to choose between insert: 1) name 2) surname 3) address

choosing surname he have to write his surname and i want to store it into a variabile but, if i use $update = file_get_contents('php://input') i always read "surname" (which is the frist input of the user)

So i think that my problem is: how to change the content of file_get_contents('php://input') in a partical "moment" of my program?

Please read my code for more details, many thanks!!!

    <?php
$botToken = "MYTOKEN"; //token 
$website = "https://api.telegram.org/bot".$botToken;
$update = file_get_contents('php://input'); //updates from telegram (JSON format)
$update = json_decode($update, TRUE); 

$chatId = $update['message']['from']['id']; 
$text = $update['message']['text']; 

switch($text)
    {
    case "name";
    sendMessage ($chatId,"You write name");
    break;

    case "surname";
    sendMessage ($chatId,"You write surname"); 
    sendMessage ($chatId,"Insert your surname"); 
                /* here is my problem: what i have to do to "read" what the 
                user write now? i want to store his surname into a new 
                variable*/
    break;

    case "address";
    sendMessage ($chatId,"You write address");
    break; 

    default;
    sendMessage ($chatId,"You don't write a valid command");
    break;     
    }


function sendMessage($chatId,$text)
    {
        $url = $GLOBALS[website]."/sendMessage? 
chat_id=$chatId&text=".urlencode($text);
        file_get_contents($url);
    }
?>
Marc Kerf
  • 3
  • 2

1 Answers1

0

I'm one of those who would "hack" this with Javascript to send just the data entered and the HTML element it was entered in but you are asking specifically for php://input.

Basically, php://input returns all the raw data after the HTTP-headers of the request, regardless of the content type.

So you could use XPath or domdocument (https://www.php.net/manual/en/class.domdocument.php) to find an element by its ID from the content you got from the "php://input" stream.

Catar4
  • 198
  • 11
  • Hi! Thank you for your reply... So you mean that everything a user will write will be "saved" into php://input, right? At this point i have to check into this and search for the second input of the user, right? thanks!!! – Marc Kerf May 24 '19 at 07:35
  • Yeah basically, php://input seems to give you "access" to the DOM. Having not used the domdocument class myself much (I generally use Javascript or XmlHttpRequest for that) I simply read the documentation and I would use the "domdocument.getElementById("theId")" function for that. https://www.php.net/manual/en/domdocument.getelementbyid.php In short, you "fetch" the DOM by: $content = get_file_contents("php://input"); and then you create a domdocument and use the "loadHTML($content)" function to load the content of your HTML form sent by the client. See the 2nd ex in that link. – Catar4 May 24 '19 at 12:59
  • Thank you very mich! Solved!! :) – Marc Kerf May 26 '19 at 22:42