Telegram BOT Api: how to send a photo using PHP?

后端 未结 6 1899
臣服心动
臣服心动 2020-12-24 09:32

The sendPhoto command require an argument photo defined as InputFile or String.

The API doc tells:

Photo to send. You can          


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 10:08

    This is my working solution, but it requires PHP 5.5:

    $bot_url    = "https://api.telegram.org/bot/";
    $url        = $bot_url . "sendPhoto?chat_id=" . $chat_id ;
    
    $post_fields = array('chat_id'   => $chat_id,
        'photo'     => new CURLFile(realpath("/path/to/image.png"))
    );
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type:multipart/form-data"
    ));
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
    $output = curl_exec($ch);
    

提交回复
热议问题