2016-05-21 5 views
0

Ich brauche Fotos von einer externen Adresse zu meinem Kanal senden:Wie kann ich Fotos von einer externen Adresse an meinen Kanal senden?

**for example :**

und ich habe einen Code für Sende Text Kanal:

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

Wie kann ich Fotos von externer Adresse an meinen Kanal senden? (In getUpdates Methode)

+0

gerettet werden könnten Sie die Technologie auf den Titel hinzufügen, die Sie verwenden und was tut es bedeutet "Kanal"? – fjuan

Antwort

0

Zuerst müssen Sie die Datei auf dem Server speichern, zum Beispiel mit file_get_contents und 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 

Anschließend können Sie das Bild schreiben Sie auf das Telegramm api gespeichert

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

@Maak
es ist sehr gut, aber wenn ich meinen Code ändern:

function goTelegram($message) { 
 

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

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

 
\t \t \t return $response; 
 
\t \t } 
 

 
$imgUrl = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"; 
 
$path = "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 
 

 

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

und meine Seite aktualisieren, der Roboter nicht senden Foto zu meinem Kanal
und Bilder können nicht

+0

Wenn Sie 'bot_token' und 'channel_name' nur innerhalb der Funktion definieren, wird es danach nicht definiert. Was für einen Fehler hast du? – Maak

+0

@Maak, wenn ich diesen Code ausführen, Bot kann es nicht an meinen Kanal senden –