简体   繁体   English

用PHP开发的Telegram机器人的问题

[英]Issue with Telegram bot developed in PHP

About a couple of months ago I could upload photos from my bot path with 5.5 version. 大约几个月前,我可以从机器人路径上传5.5版本的照片。 Now I upgraded my PHP to 5.6 and I don't know why but I can't anymore. 现在,我将PHP升级到5.6,我不知道为什么,但是现在不能了。 This was my code: 这是我的代码:

$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);

Use this function i wrote for CURL : 使用我为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);
    }
}

Then call it wherever you want : 然后在任何地方调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM