首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >电报sendPhoto多部分/表格-数据不工作?

电报sendPhoto多部分/表格-数据不工作?
EN

Stack Overflow用户
提问于 2022-07-01 09:44:36
回答 1查看 200关注 0票数 0

嗨,我正试着发一张照片。文档声明我可以使用多部分/表单数据发送文件。

这是我的代码:

代码语言:javascript
复制
// I checked it, there really is a file.
$file = File::get(Storage::disk('local')->path('test.jpeg')) // it's the same as file_get_contents();

// Here I use the longman/telegram-bot library.
$serverResponse = Request::sendPhoto([
    'chat_id' => $this->tg_user_chat_id,
    'photo' => $file
]);

// Here I use Guzzle because I thought that there might be an 
// error due to the longman/telegram-bot library.
$client = new Client();
$response = $client->post("https://api.telegram.org/$telegram->botToken/sendPhoto", [
    'multipart' => [
        [
            'name'     => 'photo',
            'contents' => $file
        ],
        [
            'name'     => 'chat_id',
            'contents' => $this->tg_user_chat_id
        ]
    ]
]);

Log::info('_response', ['_' => $response->getBody()]);
Log::info(env('APP_URL') . "/storage/$url");
Log::info('response:', ['_' => $serverResponse->getResult()]);
Log::info('ok:', ['_' => $serverResponse->getOk()]);
Log::info('error_code:', ['_' => $serverResponse->getErrorCode()]);
Log::info('raw_data:', ['_' => $serverResponse->getRawData()]);

在这两种情况下,我都得到了这样的答复:

代码语言:javascript
复制
{\"ok\":false,\"error_code\":400,\"description\":\"Bad Request: invalid file HTTP URL specified: Wrong URL host\"}

其他下载方法(通过ID和链接)工作。谁能给我指出正确的方向吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-01 10:03:37

使用php-telegram-bot 库,可以如下所示使用sendPhoto

代码语言:javascript
复制
<?php

    require __DIR__ . '/vendor/autoload.php';
    use Longman\TelegramBot\Telegram;
    use Longman\TelegramBot\Request;

    // File
    $file = Request::encodeFile('/tmp/image.jpeg');

    // Bot
    $key = '859163076:something';
    $telegram = new Telegram($key);

    // sendPhoto
    $chatId = 000001;
    $serverResponse = Request::sendPhoto([
        'chat_id' => $chatId,
        'photo' => $file
    ]);

诀窍是使用Request::encodeFile读取本地图像。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72827390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档