微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

电报BOT Api:如何使用PHP发送照片?

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

原文地址:https://www.jb51.cc/php/136944.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐