电报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);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...