Telegram Bot API sendPhoto网址包含像右单引号(')一样的字符

问题描述 投票:0回答:1

我正在使用标准的Telegram Bot API发送消息。当照片网址中包含特殊字符(如右单引号(')和左单引号('))时,该消息将不发送,因为它是错误的请求。运行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个回答
0
投票
$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编码(甚至可以第二次对其进行编码)。 
© www.soinside.com 2019 - 2024. All rights reserved.