简体   繁体   中英

curl post multipart/form-data api

I can not send pictures.

More information - Telegram
https://core.telegram.org/bots/api#sendphoto

I want to send a picture, this method is useless. Are there any other method?

    <?php
       $Photo = "http://www.pawprint.net/images/news/1-4fac83467069c.png";
       $IDUser = 26034352;
        $Data = array(
        'chat_id' => $IDUser,
        'photo' => $Photo,
        'caption' => 'hi'
        );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://api.telegram.org/bot93816942:AAHnjqsjpJjRItc7ySbUq4C5IRLqytpPK6k/sendPhoto");
    curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS,$options);
    // in real life you should use something like:
     curl_setopt($ch, CURLOPT_POSTFIELDS, 
              http_build_query($Data));
    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec ($ch);
    curl_close ($ch);
    // frther processing ....
    if ($server_output == "OK") { 
        echo "ok";
    }
    ?>

I see you want to send a picture located on a external server with its URL so you can do as follow

define('website', 'https://api.telegram.org/bot<bot-token>');

    function send($method, $datas)
    {
        $url = website . "/" . $method;
        if (!$curld = curl_init()) {
            exit;
        }
        curl_setopt($curld, CURLOPT_POST, true);
        curl_setopt($curld, CURLOPT_POSTFIELDS, $datas);
        curl_setopt($curld, CURLOPT_URL, $url);
        curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($curld);
        curl_close($curld);
    }

now just call this function with proper method name and data. example for sending a photo:

$postfields = array(
   'chat_id' => $ChatID,
   'photo' => "http://url.of/photo.jpg"
);

send("sendPhoto", $postfields);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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