URLencode如何在curl PHP中使用参数?

问题描述 投票:-1回答:2

我对urlencode有一个非常奇怪的问题,现在我可以解释:

我有一个电报自动程序,使用url向我发送消息,因此,出于这个原因,我想添加urlencode我要粘贴在url中的文本。

但是如果使用CURLOPT_POSTFIELDS,我会遇到奇怪的问题。


要发送的消息是:

  This is an example my friend

但是如果使用urlencodeCURLOPT_POSTFIELDS,则输出为:

  This+is+an+example+my+friend

现在我显示完整代码:

$notifica= urlencode("This is an example my friend");
sendMessage(xxx, $notifica);
sendMessage_2($notifica);


function sendMessage($chat_id, $message){           
   $params=[
    'chat_id' => $chat_id,
    'parse_mode' => 'HTML',
    'text' => $message
   ];

   $url= 'https://api.telegram.org/botxxx';
   $ch = curl_init($url);
   curl_setopt($ch, CURLOPT_HEADER, false);
   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3500);
   curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3500);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   $result = curl_exec($ch);
   curl_close($ch); 
}

function sendMessage_2($mess){          
    $url= 'https://api.telegram.org/botxxx/sendMessage?chat_id=xxx&parse_mode=HTML&text='.$mess;

    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3500);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3500);
    $result = curl_exec($ch);
    curl_close($ch);    
}

我希望有人可以帮助我...非常感谢,对不起,我的英语

php curl telegram telegram-bot urlencode
2个回答
0
投票

尝试使用curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));


0
投票

[将C0]放入数组中时,请勿使用urlencode()CURLOPT_POSTFIELDS说:

如果documentation是数组,则Content-Type标头将设置为multipart / form-data

此格式不需要URL编码,因此服务器不会自动对其进行解码。结果,将按字面对待编码中使用的字符(空格编码为value,其他特殊字符使用+后跟十六进制代码)。

© www.soinside.com 2019 - 2024. All rights reserved.