Skip to content

Instantly share code, notes, and snippets.

@layerok
Last active May 17, 2025 21:29
Show Gist options
  • Save layerok/20f6f09d863155103cd5421320b47d0e to your computer and use it in GitHub Desktop.
Save layerok/20f6f09d863155103cd5421320b47d0e to your computer and use it in GitHub Desktop.
send telegram message curl php
<?php
function sendTelegramMessage($chatID, $message, $token) {
$url = "https://api.telegram.org/bot" . $token . "/sendMessage";
$data = ['chat_id' => $chatID, 'text' => $message, 'parse_mode' => 'HTML'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_FAILONERROR, 0); // Do not fail on HTTP errors
$response = curl_exec($ch);
// here you can Handle cURL error
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
curl_close($ch);
return json_encode(array('error' => $error_msg));
}
$result = json_decode($response, true);
if ($result['ok']) {
$result = json_encode(array('success' => $result['ok']));
} else {
$result = json_encode(array('error' => $result));
}
curl_close($ch);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment