发送内联查询结果错误(Telegram)


send inline query result error (Telegram)

我想制作一个内联机器人!当我这样做的时候:

function sendResponse($url, $data){
    $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_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('inline_query_id' => $data['inline_query_id'], 'results' => json_encode($data['results'])));
    $output = curl_exec($ch);
    return $output;
}

它不起作用,错误(有或没有标题):{"ok":false,"error_code":400,"description":"[Error]: Bad request: Field '"message_text'" must be of type String"}

但是当我这样做的时候:

function sendResponse($url, $data){
    $ch = curl_init();
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
    curl_setopt($ch, CURLOPT_URL, $url.'?inline_query_id='.rawurlencode($data['inline_query_id']).'&results='.rawurlencode(json_encode($data['results']))); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    //curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $q);
    $output = curl_exec($ch);
    return $output;
}

它有效!问题是第二个方法请求URI太大,所以我无法使用它!

我可以用任何方式发送这些数据!谢谢

制作$data的代码在这里:

$result = connectWebsite(SITE_SEARCH_URL, urlencode($update['inline_query']['query']));
$result = json_decode($result);
$output = array();
$output['inline_query_id'] = $update['inline_query']['id'];
$i = 0;
foreach($result as $post){
    $data = array();
    $data['type'] = 'article';
    $data['id'] = strval($post->ID);
    $data['title'] = '('.$post->atypes.') '.$post->title;
    if(strlen($post->content) > 2100)
        $tmp = substr($post->content, 0, 2096).'...';
    $data['message_text'] = '<b>'.$post->title.'</b>'.ucwords($post->genre, ',').$tmp;
    $data['parse_mode'] = 'HTML';
    if(strlen($post->content) > 200)
        $tmp = substr($post->content, 0, 196).'...';
    //$data['description'] = ucwords($post->genre, ',').'  |  '.$tmp;
    $output['results'][$i] = $data;
    $i++;
    if($i == MAX_RESULTS)
        break;
}   
sendResponse(API_URL.'answerInlineQuery', $output);

这可能会对某人有所帮助,所以我会自己回答。

问题是UTF-8编码的

我用mb_substr 替换了substr

此外,在第一行我添加了以下内容:mb_internal_encoding("UTF-8")

而且。。。问题解决了。现在我可以发送我的内联查询结果(或任何其他命令),而不会出现URL长度问题

感谢大家的帮助