skip to Main Content

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:

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

?>

error :

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:xampphtdocsmgp25Telegram-Bot-API-mastersrcTelegram.php on line 465

What’s wrong with my code?

2

Answers


  1. I think urlencode(‘http://127.0.0.1/mgp25/maldini.jpg‘); is the problem. You should use your provider/public IP address. Or use a relative path.

    Login or Signup to reply.
  2. 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 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search