hello guys, I know you may heard about Telegram bot API, and I want to use their API to send texts and photos, telegram API differs from bots API here is the full document about API https://core.telegram.org/bots/api
I want to send photo using curl but its fail always, sending text is easy since it take string patameters
$sendmsg = "https://api.telegram.org/bot".$token."/sendMessage?chat_id=".$chat_id."&text=".$text."";
here is my try to send photos but it failes always, no error messages just no photos sent

if (isset($_POST['btnUpload']))
$token = <token here>;
$chat_id = "17446522";
$text = " Test ";
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];
if ($filedata != '')
    $url = 'https://api.telegram.org/bot'.$token.'/sendPhoto';
$data = array('chat_id' => $chat_id, 'photo' => $filedata);
function multipart_build_query($fields, $boundary){
$retval = '';
foreach($fields as $key => $value){
$retval .= "--$boundary\r\nContent-Disposition: form-data; name=\"$key\"\r\n\r\n$value\r\n";
$retval .= "--$boundary--";
return $retval;
$boundary = '--myboundary-xxx';
$body = multipart_build_query($data, $boundary);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
$message = json_decode($response, true);
 echo "<pre>";
 echo "</pre>";
 <form action="" method="post" enctype="multipart/form-data">
  <td align="center">:</td>
  <td><input name="file" type="file" id="file"/></td>
  <td align="center">&nbsp;</td>
  <td><input name="btnUpload" type="submit" value="Upload" /></td>
2 Years
Discussion Span
Last Post by cereal


It happens because you are not appending the file to the body of the request, if you open netcat on localhost:8005:

nc -l localhost 8005

And change the $url to:

$url = 'http://localhost:8005/';

You can see what is sent by curl. In your case this is what is sent:

Host: localhost:8005
Accept: */*
Content-Type: multipart/form-data; boundary=--myboundary-xxx
Content-Length: 184

Content-Disposition: form-data; name="chat_id"

Content-Disposition: form-data; name="photo"


As you see it's displaying the filename of the uploaded file, not the binary, use file_get_contents():

$filedata = file_get_contents($_FILES['file']['tmp_name']);

And then it should work fine.

By the way: it seems you're missing the closing curly bracket for this statement if ($filedata != ''), around line 33.

Edited by cereal


@cereal for closing curly bracket its just a typo from copying code,
I've change $filedata as you told me, still nothing happened, when I output curl info here is what I got

{"ok":false,"error_code":400,"description":"Error: Bad Request: Wrong persistent file_id specified: contains wrong characters or have wrong length"}

POST /bot<token>/sendPhoto HTTP/1.1
Host: api.telegram.org
Accept: */*
Content-Type: multipart/form-data; boundary=--myboundary-xxx
Content-Length: 39617
Expect: 100-continue

When using only html its worked :P

    <form method="POST" action="https://api.telegram.org/bot<token>/sendPhoto" enctype="multipart/form-data">
            <span>chat_id :</span>
            <input id="chat_id" type="text" name="chat_id" value="17446522" />

            <span>caption :</span>
            <input id="caption" type="text" name="caption"/>

            <input id="photo" type="file" name="photo" />

            <input type="submit" class="button" value="sendPhoto" />

but if I have 100 subscriber this method is not good.


Ok, at the moment I cannot test their API, but I see from your request that curl will set Expect: 100-continue, if their server does not reply HTTP/1.1 100 Continue then curl will not send the body.

You can try to fix it by adding Expect: (with a blank space after the colon character :) to the CURLOPT_HTTPHEADER array, this should unset the 100-continue.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.