如何将外部地址的照片发送到我的频道?

时间:2016-05-21 14:49:08

标签: php telegram-bot

我需要将外部地址的照片发送到我的频道:

**for example :**

我有一个代码,用于将文本发送到频道:

$bot_token = '*****';
$channel_name = '@******';
$content = urlencode("{$message}");
$url = "https://api.telegram.org/bot{$bot_token}/sendMessage?chat_id={$channel_name}&parse_mode=html&disable_web_page_preview=false&text={$content}";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);

return $response;

如何将外部地址的照片发送到我的频道? (在getUpdates方法中)

1 个答案:

答案 0 :(得分:0)

您需要先将文件保存在服务器上,例如file_get_contentsfile_put_contents

$imgUrl = "<image url>";
$path = "tmp/img.png"; //location on your server to save the image
$image = file_get_contents($imgURL); //get the image
file_put_contents($path, $image); //save the image on your server

之后,您可以将保存的图像发布到电报api

$url = "https://api.telegram.org/bot" . $bot_token  . "/sendPhoto";
$filepath = realpath($path);
//Set post data
$post = array('chat_id' => $channel_name ,'photo'=>new CURLFile($filepath));    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec ($ch);
curl_close ($ch);