Php 电报中的SendPhoto方法不起作用

Php 电报中的SendPhoto方法不起作用,php,curl,telegram,Php,Curl,Telegram,我想通过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" =>

我想通过
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);
但它不能发送照片。

我在不同的网站上寻找解决方案,但他们的代码与我的代码相同。


为什么不工作?

这是我的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;


}   
sendPicture($id,“picture.png”);
函数sendPicture($\u chatID,$file\u在服务器上){
$target\u url='1https://api.telegram.org/****/发送照片';
$file_name_,其中_full_path=realpath('./'.$file_在_服务器上);
$post=数组(
'chat\u id'=>$\u chatID,
'photo'=>'@.$file\u name\u带有完整路径
);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$target_URL);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl\u exec($ch);
卷曲关闭($ch);
回声$结果;
}   

谢谢@Markus,但它也不能发送照片<代码>{“ok”:false,“error_code”:400,“description:“error:错误的请求:错误的持久文件\u id指定:包含错误的字符或长度错误”}@Alireza:在我看来,似乎是请求的编码有问题。电报机器人API仅使用UTF-8。所以您可以尝试一下,将您的编码设置为UTF-8:iconv(mb_detect_encoding($text,mb_detect_order(),true),“UTF-8”,$text);照片不在电报服务器中。我们应该先上传。但我们没有!我的代码将图像从我的Web服务器发布到电报服务器。我可以在GetUpdate中看到该事件。图片成为一个唯一的id。我的意思是,图片首先在我们的服务器中,我们应该通过一个Mulitparat/data文件头发送它。我认为这个解决方案可能会解决您的问题,并提供帮助