总是无法使用PHP使用电报API发送图像 [英] always failed to send image using telegram API using PHP

查看:130
本文介绍了总是无法使用PHP使用电报API发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用电报API发送图像的函数(参考API: https: //github.com/mgp25/Telegram-Bot-API/),但是当我尝试运行此代码时,总是会收到如下错误:

I want to make a function to send an image using Telegram API (reference API: https://github.com/mgp25/Telegram-Bot-API/), but when I try to run this, I always get an error like this:

消息:file_get_contents(''):无法打开流:HTTP请求失败! HTTP/1.1 400错误请求

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

这是我的代码[更新]:

Here is my code [updated]:

<?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行

Warning: file_get_contents(https://api.telegram.org/bot_token/sendPhoto?chat_id=chat_id&photo=0): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\mgp25\Telegram-Bot-API-master\src\Telegram.php on line 465

我的代码怎么了?

推荐答案

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

Do you have SSL connection with Telegram? If you did not have SSL connection with telegram, non of telegram commands will not work but if you can send just a simple message so there is no SSL problem. After all if everything is OK except image,use this cURL code except using that ready to use telegramBOT class. if this is not working (cURL) , so there is really problem in reading from or finding photo on your server(Real server or xampp folder or so on...) If it is server(host) it MUST be upload first and if it is xampp image must be in true folder. it is better to test if image is accessible (for example via http://localhost/image/maldini.jpg from webbrowser?

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

cURL Ready to use code for sending photo:

$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);

这篇关于总是无法使用PHP使用电报API发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆