Reputation: 149
I want to send message via telegram api
but it's not working, and not send any message. this is what i tried so far:
function sendTelegram($chatID, $msg) {
echo "sending message to " . $chatID . "\n";
$token = "botxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$getUpdate = "http://api.telegram.org/" . $token . "/getUpdates";
$url = "https://api.telegram.org/" . $token . "/sendMessage?chat_id=" . $chatID;
$url = $url . "&text=" . urlencode($msg);
$ch = curl_init();
$optArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);
curl_close($ch);
}
$msg = "Hi";
$chatID = "88132232";
sendTelegram($chatID, $msg);
My progress:
new bot
via @botfather
and got a token
.bot
with my telegram
.chat id
in getUpdates
.https://api.telegram.org/botxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/getUpdates
and also send message
via:
https://api.telegram.org/botxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/sendMessage?chat_id=88132232&text=hi
It works find when i go to this url but when i want to do this dynamically it give me nothing just echo sending message to 88132232
with no error. I searched and read many topics but no success, any idea what i missed? Before using curl
i used get_file_contents
but it also not worked.
Upvotes: 1
Views: 2451
Reputation: 16575
Probably you have error
but in curl
you should get curl
error like this:
if(curl_error($ch)){
echo 'error:' . curl_error($ch);
}
And most problem is SSL
. get your error and back. but i tested your code, as @Sean said, your code working fine, try it on php fiddle
website. if you get SSL
error, read this.
Upvotes: 1
Reputation: 7985
You set CURLOPT_RETURNTRANSFER
CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
Please return $result
in sendTelegram()
function, and echo it.
function sendTelegram($chatID, $msg) {
// ...
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = sendTelegram($chatID, $msg);
echo $result; // JSON String
Upvotes: 1