doubeng9407 2016-07-04 18:19
浏览 44

使用电报API询问发送图像失败?

now i'm work with telegram API. i want send an image with this API but my code doesnt work, when i run it, i have a blank response.

here is my code :

<?php




$comment= $_POST['tag'];
$url = 'https://api.telegram.org/botMY_BOT_ID/sendPhoto';

try {
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"));
curl_setopt($curl_connection, CURLOPT_URL, $url); 
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, http_build_query(array('chat_id'=>'chatid','photo' => "@"."maldini.jpg")));
//curl_setopt($curl_connection, CURLOPT_INFILESIZE, filesize("path/to/maldini.jpg"));
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);

//Data are stored in $data
$data = curl_exec($curl_connection);

curl_close($curl_connection);
} catch(Exception $e) {
return $e->getMessage();
}

?>

but, when i try send image with just html, i works perfectly. here is my html code :

        <form method="POST" action="https://api.telegram.org/botMY_BOT_ID/sendPhoto" enctype="multipart/form-data">
        <label>
            <span>chat_id :</span>
            <input id="chat_id" type="text" name="chat_id" value="chat_id" />
        </label>
        <label>
            <span>caption :</span>
            <input id="caption" type="text" name="caption"/>
        </label>
        <label>
            <span>photo</span>
            <input id="photo" type="file" name="photo" />
        </label>      
         <label>
            <span>&nbsp;</span>
            <input type="submit" class="button" value="sendPhoto" />
        </label>    
    </form>

whats wrong??? any help would be appreciate, thanks in advance :)

  • 写回答

1条回答 默认 最新

  • douji1999 2016-07-05 11:43
    关注

    just need to pass chat_id with the url, try this code

    <?php 
    $url = "https://api.telegram.org/bot<bot_id>/sendPhoto?chat_id=".$chat_id ;
    
    $post_fields = array('chat_id' => $chat_id,'photo'=> "/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);
    echo "<pre>"; print_r($output);
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题