php - curl PHP中参数的URLENCODE如何工作?
问题描述
我有一个非常奇怪的问题,urlencode
现在我解释一下:
我有一个电报机器人,可以通过 url 向我发送消息,因此我想添加urlencode
我将粘贴到 url 中的 fot 文本。
但如果使用CURLOPT_POSTFIELDS
我有一个奇怪的问题。
要发送的消息是:
This is an example my friend
但是如果使用urlencode
并且CURLOPT_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);
}
我希望有人可以帮助我......非常感谢,对不起我的英语
解决方案
urlencode()
当您将参数放入带有CURLOPT_POSTFIELDS
. 文档说:
如果
value
是数组,则Content-Type标头将设置为multipart/form-data。
这种格式不需要 URL 编码,所以服务器不会自动解码。因此,编码中使用的字符(空格编码为+
,其他使用的特殊字符%
后跟十六进制代码)将按字面处理。
推荐阅读
- java - 从 Set 获取对象数组的问题
在安卓中 - ruby - As1851Tests#new 中的 NoMethodError 与浅路径
- java - 当抽象类中的方法调用已被覆盖的方法时,继承如何在 java 中工作?
- angular - 部署在 Cloud Foundry 中时的 Angular 4 应用程序路由问题
- php - Dialogflow API:从检测意图响应中检索提取的参数
- android - Gradle 在添加 firebase 时出现兼容性错误
- javascript - 从 Android 调用 JavaScript
- slurm - 为单台计算机设置 slurm.conf 文件
- java - List 中是否有任何类型的 reverse_iterator,只是为了反转 List?(Java8 流解决方案优先)
- android - Android 矢量资源太大