简体   繁体   中英

How can I send photos from external address to my channel?

I need send photos from an external address to my channel:

**例如 :**

and I have a code for send text to channel:

$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;

How can I send photos from external address to my channel?? (in getUpdates method)

You need to save the file on your server first, for example with file_get_contents and file_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

Afterwards you can post the image you saved to the telegram 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM