【问题标题】:Telegram Bot API sendPhoto url contains characters like right single quotation mark (’)Telegram Bot API sendPhoto url 包含右单引号 (') 等字符
【发布时间】:2020-09-17 01:11:39
【问题描述】:

我正在使用标准 Telegram Bot API 发送消息。当照片 url 包含像右单引号 (') 和左单引号 (') 这样的特殊字符时,不会发送消息,因为它是错误请求。 运行 php rawurlencode 时,’ 显示为 %E2%80%99 和 ‘ 显示为 %E2%80%98

$photourl='https://example.com/some/path/name-of-image-with‘quotes’.jpg';
$array =[
'chat_id' => $uid,
'photo' => $photourl,
'caption' => $caption
];
file_get_contents("https://api.telegram.org/bot$api/sendPhoto?".http_build_query($array) ); 

它给出了一个

PHP 警告 -- 无法打开流:HTTP 请求失败! HTTP/1.1 400 错误请求

【问题讨论】:

    标签: url telegram-bot sendmessage


    【解决方案1】:

    试试这个:

    $photourl = 'https://example.com/some/path/name-of-image-with‘quotes’.jpg';
    $filename = basename($photourl);
    $photourl = str_replace($filename, urlencode($filename), $photourl); //Replace file name wirth url encoded filename
    
    $array = [
        'chat_id' => $uid,
        'photo' => $photourl,
        'caption' => $caption
    ];
    file_get_contents("https://api.telegram.org/bot$api/sendPhoto?" . http_build_query($array));
    

    在photo 参数中使用’、‘ 或* 等字符似乎会导致问题。

    我没有任何解释为什么会发生此错误或如何修复它。但是您可以在将查询传递给http_build_query() 之前对 URL 的文件名部分进行 url 编码(这甚至会对其进行第二次编码)。

    【讨论】:

    • 谢谢它似乎工作。我会彻底测试一下。
    猜你喜欢
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2018-04-07
    相关资源
    最近更新 更多