使用 PHP 使用电报 API 始终无法发送图像

always failed to send image using telegram API using PHP

我想创建一个使用 Telegram API 发送图像的功能(参考 API:https://github.com/mgp25/Telegram-Bot-API/),但是当我尝试 运行 这个时,我总是得到这样的错误:

Message: file_get_contents(''): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

这是我的代码[更新]:

<?php

require 'Telegram.php';

$tele = new telegramBot('token');
//$info = $tele->sendMessage('218945828',"wadaw");
$url= 'image/maldini.jpg';
$info = $tele->sendPhoto('chatid',$url);
print_r($info);

?>

错误:

警告:file_get_contents(https://api.telegram.org/bot_token/sendPhoto?chat_id=chat_id&photo=0):无法打开流:php_network_getaddresses:getaddrinfo 失败:不知道这样的主机。在 C:\xampp\htdocs\mgp25\Telegram-Bot-API-master\src\Telegram.php 第 465 行

我的代码有什么问题?

我认为是 urlencode('http://127.0.0.1/mgp25/maldini.jpg');是问题所在。您应该使用 provider/public IP 地址。或者使用相对路径。

你有 Telegram 的 SSL 连接吗?如果您没有与电报的 SSL 连接,则非电报命令将不起作用,但如果您只能发送一条简单的消息,那么就不存在 SSL 问题。 毕竟,如果除了图像之外一切正常,请使用此 cURL 代码,除非使用准备使用的 telegramBOT class。 如果这不起作用 (cURL) ,那么在您的服务器上读取或查找照片确实存在问题(真实服务器或 xampp 文件夹等等...) 如果是服务器(主机),则必须先上传,如果是 xampp,图像必须位于真实文件夹中。最好测试图像是否可访问(例如通过 http://localhost/image/maldini.jpg 来自 webbrowser?

cURL 准备使用代码发送照片:

$BOT_TOKEN='1231325:AbXDECcvhir7'; //----YOUR BOT TOKEN
$chat_id=123456 // or '123456' ------Receiver chat id
define('BOTAPI','https://api.telegram.org/bot' . $BOT_TOKEN .'/');

$cfile = new CURLFile(realpath('image/maldini.jpg'), 'image/jpg', 'maldini.jpg'); //first parameter is YOUR IMAGE path
    $data = [
        'chat_id' => $chat_id , 
        'photo' => $cfile
        ];

    $ch = curl_init(BOTAPI.'sendPhoto');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);