S
S
suchrile2021-08-23 02:02:12
PHP
suchrile, 2021-08-23 02:02:12

Failed to open stream: HTTP request failed! What is the reason?

Good evening!

Problem with sending data via Telegram API.
In a PHP file, I receive input values ​​sent via AJAX and rows from the database by id. I write these strings to an array and then convert it to a string using implode();
This data is then collected into an array, converted to a string, and sent via fopen. If everything is fine with the received input values, then when I try to send the string received from the database, I get the error:
failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request on line:

$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");

Please tell me how to solve the problem.

PHP code:
foreach ($idItems as $id) {
        $conn = connect();
        $query = mysqli_query($conn, "SELECT title FROM `catalog` WHERE id = $id");

        while($result = mysqli_fetch_assoc($query)) {
            $title[] = $result['title'];
        }
    }

    $title = implode(", ", $title);     
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $city = $_POST['city'];
    $address = $_POST['address'];
    $index = $_POST['index'];

    $arr = array(
    'Покупатель: ' => $name,
    'Телефон: ' => $phone,
    'Email: ' => $email,
    'Город: ' => $city,
    'Адрес: ' => $address,
    'Индекс: ' => $index,
    'Товар: ' => $title
    );

    foreach($arr as $key => $value) {
    $txt .= "<b>".$key."</b> ".$value."%0A";
    };

    $sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
suchrile, 2021-08-23
@suchrile

After several hours of searching for a solution, it turned out that the problem was in the space after the tag in this line: Why everything worked without the title variable remains a mystery... </b>
$txt .= "<b>".$key."</b> ".$value."%0A";

G
galaxy, 2021-08-23
@galaxy

Try$txt = urlencode($txt);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question