B
B
betored2020-12-04 05:49:31
PHP
betored, 2020-12-04 05:49:31

Sending a form in telegram what's wrong?

I sent data to telegram, everything worked, and suddenly

the code stopped working once:

<?php


$aipi = $_SERVER['REMOTE_ADDR'];

$token = "1420262397:AAFJZEK-q3_ba3cYcqbUo9R2cz1wUCEiNcg";
$chat_id = "-253031623";



$client  = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote  = @$_SERVER['REMOTE_ADDR'];
$result  = array('country'=>'', 'city'=>'');
 
if(filter_var($client, FILTER_VALIDATE_IP)) $ip = $client;
elseif(filter_var($forward, FILTER_VALIDATE_IP)) $ip = $forward;
else $ip = $remote;
 
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));    
if($ip_data && $ip_data->geoplugin_countryName != null)
{
    $result = $ip_data->geoplugin_countryCode;
}
 





$arr = array(
  'Кто то перешёл на сайт! IP: ' => $aipi,
  'Страна: ' => $result,
  
);

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

if ($sendToTelegram) {
  header('Location: testuser.php');
} else {
  echo "Error";
}
?>


error:

Notice: Undefined variable: txt in /sites/site.com/index.php on line 38

Warning: fopen( https://api.telegram.org/bot11420262397:AAFJZEK-q3... Someone went to the site! IP: 123.123.123.123%0A Country: RU%0A): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /sites/site.com/index.php on line 41
Error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Gorelov, 2020-12-04
@betored

We need to declare a variable first. And then look further.

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question