我想制作一个使用 Telegram API 发送图像的函数(参考 API:https://github.com/mgp25/Telegram-Bot-API/ https://github.com/mgp25/Telegram-Bot-API/),但是当我尝试运行它时,我总是收到如下错误:
消息:file_get_contents(''):无法打开流:HTTP 请求失败! HTTP/1.1 400 错误请求
这是我的代码[已更新]:
<?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);
?>
error :
警告:file_get_contents(): 无法打开流: php_network_getaddresses: getaddrinfo 失败: 没有这样的主机已知。在 C:\xampp\htdocs\mgp25\Telegram-Bot-API-master\src\Telegram.php 第 465 行
我的代码有什么问题吗?
您与 Telegram 有 SSL 连接吗?如果您没有与 telegram 建立 SSL 连接,则所有 telegram 命令都将不起作用,但如果您只能发送一条简单的消息,那么就不存在 SSL 问题。
毕竟,如果除了图像之外一切正常,请使用此 cURL 代码,除非使用准备使用的代码telegramBOT
班级。
如果这不起作用(cURL),那么在服务器上读取或查找照片时确实存在问题(真实服务器或 xampp 文件夹等...)
如果是服务器(主机),则必须首先上传,如果是 xampp,则图像必须位于真实文件夹中。最好测试图像是否可访问(例如通过http://localhost/image/maldini.jpg http://localhost/image/maldini.jpg从网络浏览器?
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)