Telegram BOT Api: how to send a photo using PHP?
This is my working solution, but it requires PHP 5.5:
$bot_url = "https://api.telegram.org/bot<bot_id>/";$url = $bot_url . "sendPhoto?chat_id=" . $chat_id ;$post_fields = array('chat_id' => $chat_id, 'photo' => new CURLFile(realpath("/path/to/image.png")));$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);
This code helps me alot which I get from php.net website here
Visit http://php.net/manual/en/class.curlfile.php#115161(Vote Up this code in php website).
I just change headers in this code for telegram bot to send image just copy this function
function curl_custom_postfields($ch, array $assoc = array(), array $files = array()) { // invalid characters for "name" and "filename" static $disallow = array("\0", "\"", "\r", "\n"); // build normal parameters foreach ($assoc as $k => $v) { $k = str_replace($disallow, "_", $k); $body[] = implode("\r\n", array( "Content-Disposition: form-data; name=\"{$k}\"", "", filter_var($v), )); } // build file parameters foreach ($files as $k => $v) { switch (true) { case false === $v = realpath(filter_var($v)): case !is_file($v): case !is_readable($v): continue; // or return false, throw new InvalidArgumentException } $data = file_get_contents($v); $v = call_user_func("end", explode(DIRECTORY_SEPARATOR, $v)); $k = str_replace($disallow, "_", $k); $v = str_replace($disallow, "_", $v); $body[] = implode("\r\n", array( "Content-Disposition: form-data; name=\"{$k}\"; filename=\"{$v}\"", "Content-Type: image/jpeg", "", $data, )); } // generate safe boundary do { $boundary = "---------------------" . md5(mt_rand() . microtime()); } while (preg_grep("/{$boundary}/", $body)); // add boundary for each parameters array_walk($body, function (&$part) use ($boundary) { $part = "--{$boundary}\r\n{$part}"; }); // add final boundary $body[] = "--{$boundary}--"; $body[] = ""; // set options return @curl_setopt_array($ch, array( CURLOPT_POST => true, CURLOPT_POSTFIELDS => implode("\r\n", $body), CURLOPT_HTTPHEADER => array( "Expect: 100-continue", "Content-Type: multipart/form-data; boundary={$boundary}", // change Content-Type ), )); }
Basic Try:Now just use this code by sending photo name with path and chat id here is it how:-
$array1=array('chat_id'=><here_chat_id>);$array2=array('photo'=>'index.jpg') //path$ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://api.telegram.org/<bot_token>/sendPhoto");curl_custom_postfields($ch,$array1,$array2);//above custom function$output=curl_exec($ch);close($ch);
For sending png or other methods change curl_custom function according to your need.
I searched a lot online but didn't find the answer. But, your question solved my problem ... I just changed your code and that answered it for me ...I changed your code to this:
$chat_id=chat Id Here;$bot_url = "https://api.telegram.org/botYOUR_BOT_TOKEN/";$url = $bot_url . "sendPhoto?chat_id=" . $chat_id;$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, array( "photo" => "@path/to/image.png", )); curl_setopt($ch, CURLOPT_INFILESIZE, filesize("path/to/image.png"));$output = curl_exec($ch);print$output;