1

I need some help if possible with php sendPhoto api, I've been using the sendPhoto method in php on my apache server to auto send images into telegram, I've been using this same method for almost 6-7 months and from few days ago suddenly the api method stopped working. I tried passing photo= using the absolute path of file in url and in php using the files directory+filename but sends me an error msg from the api as shown below, first part is my php method which doesnt return any errors, just shows blank

# my php telegram code

$dir = "Attachments/2022/04/09/imagename.jpeg";
$chat_id = '(groupchatid)';
$bot_url = "https://api.telegram.org/bot(mybotapi)/";
$url = $bot_url . "sendPhoto?chat_id=" . $chat_id ;
$post_fields = array('chat_id' => $chat_id,
                     'photo' => new CURLFile(realpath($dir)),
                     'caption' =>'Test Image', ); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array( "Content-Type:multipart/form-data" ));
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 

$output = curl_exec($ch);

When i execute this script as it used to work before recently this is the response i get from the API

{
  "ok": false,
  "error_code": 400,
  "description": "Bad Request: invalid file HTTP URL specified: Unsupported URL protocol"
}

If I replace the image URL to another server it send the image successfully, but im unable to send anything only from my server, If I try access the file directly using the URL of my servers image file I can access it from any pc no issue, only problem is telegram fetching the image, please help, appreciate it

midnight-coding
  • 2,857
  • 2
  • 17
  • 27
  • confused is it something with my apache server, php or telegrams api? Other methods all work ok, sendDocument, sendMessage etc.. – Stophes Jalin Apr 12 '22 at 03:19

1 Answers1

1

Excuse, I don't usually use curl, so I can give you another option:

function sendPhoto($id, $photo, $text = null){
    GLOBAL $token;
    $url = 'https://api.telegram.org/bot'.$token."/sendPhoto? 
    chat_id=$id&photo=$photo&parse_mode=HTML&caption=".urlencode($text);
    file_get_contents($url);
}

Just declare the sendPhoto function in this way, put the variabile in which you stored the token instead of "$token" and use the parameters in this way:

  • $id = the id of the user (the one you declared like this: $id = $update['message']['from']['id'];)
  • $photo = absolute path of the image you want to send
  • $text = OPTIONAL caption for the image
M1001
  • 144
  • 1
  • 11