2

我想SendPhoto在我的本地主机中使用webhook. 图片不在电报服务器中。所以我需要通过多部分标题上传它。

尝试的代码:

$file=fopen("Untitled.png","rb");
$cont=fread($file,filesize("Untitled.png"));
$headers=array("Content-type: multipart/form-data");
$postfields = array("chat_id" => "108432389", "photo" => "$file");
$ch = curl_init();
$options = array(
    CURLOPT_URL => "https://api.telegram.org/bot(Token)/SendPhoto",
    CURLOPT_HEADER => true,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $postfields,
    CURLOPT_INFILESIZE => filesize("Untitled.png"),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_HTTPHEADER => $headers
);
curl_setopt_array($ch, $options);
curl_exec($ch);

但它不能发送照片。

我在不同的网站上查看了解决方案,但是它们的代码与我的代码相同。

为什么这不起作用?

4

1 回答 1

1

这是完成这项工作的我的 PHP 函数。

sendPicture($id, "picture.png");


function sendPicture($_chatID, $file_on_server){

    $target_url = 'https://api.telegram.org/**<YOUR TOKEN HERE>**/sendphoto';

    $file_name_with_full_path = realpath('./'.$file_on_server);

    $post = array(
        'chat_id'   => $_chatID,
        'photo'     => '@'.$file_name_with_full_path
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $result=curl_exec ($ch);
    curl_close ($ch);
    echo $result;


}   
于 2015-07-21T07:42:18.677 回答