【问题标题】:SendPhoto method in telegram doesnt work电报中的 SendPhoto 方法不起作用
【发布时间】:2015-09-29 20:38:04
【问题描述】:

我想通过webhook 在我的本地主机中SendPhoto。图片不在电报服务器中。所以我需要通过multipart header上传。

尝试的代码:

$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 curl telegram


【解决方案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;


}   

【讨论】:

  • 谢谢@Markus,但它也不能发送照片。 {"ok":false,"error_code":400,"description":"Error: Bad Request: Wrong persistent file_id specified: contains wrong characters or have wrong length"}
  • @Alireza:在我看来,请求编码有问题。 Telegram bot API 仅使用 UTF-8。因此,您可以尝试一下,将您的编码设置为 UTF-8: iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);
  • 照片不在电报服务器中。我们应该先上传它。但我们没有!
  • 我的代码将图像从我的网络服务器发布到电报服务器。我可以在 getUpdates 中看到该事件。图片成为唯一的 id。
  • 我的意思是图片在我们的服务器中 首先我们应该通过 Mulitparat/data-file 头发送它,我认为
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 2022-07-02
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多