sendPhoto命令需要一个定义为InputFile或String的参数照片.
API文档告诉:
Photo to send. You can either pass a file_id as String to resend a photo that is already on the Telegram servers,or upload a new photo using multipart/form-data.
和
InputFile This object represents the contents of a file to be uploaded. Must be posted using multipart/form-data in the usual way that files are uploaded via the browser.
所以我尝试了这种方法
$bot_url = "https://api.telegram.org/bot<bot_id>/"; $url = $bot_url . "sendPhoto?chat_id=" . $chat_id; $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,array( "photo" => "@/path/to/image.png",)); curl_setopt($ch,CURLOPT_INFILESIZE,filesize("/root/dev/fe_new.png")); $output = curl_exec($ch);
卷发被执行,但Telegram回复给我:
Error: Bad Request: Wrong persistent file_id specified: contains wrong characters or have wrong length
我也尝试用file_get_contents替换@ / path …但是在这种情况下Telegram给我一个空的回复(并且curl_error是空的!).
使用PHP curl将照片发送到电报的方式是什么?
这是我的工作解决方案,但它需要PHP 5.5:
原文链接:https://www.f2er.com/php/136944.html$bot_url = "https://api.telegram.org/bot<bot_id>/"; $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,array( "Content-Type:multipart/form-data" )); curl_setopt($ch,$url); curl_setopt($ch,1); curl_setopt($ch,$post_fields); $output = curl_exec($ch);