always failed to send image using telegram API using PHP

前端 未结 2 1936
Happy的楠姐
Happy的楠姐 2021-01-26 05:04

I want to make a function to send an image using Telegram API (reference API: https://github.com/mgp25/Telegram-Bot-API/), but when I try to run this, I always get an error like

相关标签:
2条回答
  • 2021-01-26 05:37

    Do you have SSL connection with Telegram? If you did not have SSL connection with telegram, non of telegram commands will not work but if you can send just a simple message so there is no SSL problem. After all if everything is OK except image,use this cURL code except using that ready to use telegramBOT class. if this is not working (cURL) , so there is really problem in reading from or finding photo on your server(Real server or xampp folder or so on...) If it is server(host) it MUST be upload first and if it is xampp image must be in true folder. it is better to test if image is accessible (for example via http://localhost/image/maldini.jpg from webbrowser?

    cURL Ready to use code for sending photo:

    $BOT_TOKEN='1231325:AbXDECcvhir7'; //----YOUR BOT TOKEN
    $chat_id=123456 // or '123456' ------Receiver chat id
    define('BOTAPI','https://api.telegram.org/bot' . $BOT_TOKEN .'/');
    
    $cfile = new CURLFile(realpath('image/maldini.jpg'), 'image/jpg', 'maldini.jpg'); //first parameter is YOUR IMAGE path
        $data = [
            'chat_id' => $chat_id , 
            'photo' => $cfile
            ];
    
        $ch = curl_init(BOTAPI.'sendPhoto');
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        curl_close($ch);
    
    0 讨论(0)
  • 2021-01-26 05:41

    I think urlencode('http://127.0.0.1/mgp25/maldini.jpg'); is the problem. You should use your provider/public IP address. Or use a relative path.

    0 讨论(0)
提交回复
热议问题