使用PHP开发的Telegram bot问题

时间:2016-04-07 23:28:19

标签: php bots telegram telegram-bot

大约几个月前,我可以使用5.5版本从我的机器人路径上传照片。现在我将我的PHP升级到5.6,我不知道为什么,但我不能再了。 这是我的代码:

$url = "https://api.telegram.org/bot".Token."/sendPhoto?chat_id=".$chat_id;

        $post_fields = array(
                'photo'     => new CURLFile(realpath("test.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);

1 个答案:

答案 0 :(得分:0)

使用我为CURL写的这个函数:

function makeHTTPRequest($method, $types = []){
    $url = 'https://api.telegram.org/bot'.Token.'/'.$method;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($types));
    $res = curl_exec($ch);
    if (curl_error($ch)){
        var_dump(curl_error($ch));
    } else {
        return json_decode($res);
    }
}

然后在任何地方调用它:

var_dump(makeHTTPRequest('sendPhoto', [
    'chat_id' => $chat_id,
    'photo' => new CURLFile('test.png')
]));