猿问

如何在卷曲 PHP 中为参数提供网址代码?

我有非常奇怪的问题,现在我解释:urlencode


我有一个电报机器人用url向我发送消息,因此出于这个原因,我想添加我将粘贴在URL中的fot文本。urlencode


但如果使用我有奇怪的问题。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);    

}

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


心有法竹
浏览 161回答 2
2回答

哔哔one

将参数放在带有 的数组中时,请勿使用。文档说:urlencode()CURLOPT_POSTFIELDS如果 是数组,则内容类型标头将设置为多部分/表单数据。value此格式不需要 URL 编码,因此服务器不会自动对其进行解码。因此,编码中使用的字符(空格编码为 ,其他特殊字符使用后跟十六进制代码)将按字面意思处理。+%

森林海

尝试使用curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
随时随地看视频慕课网APP
我要回答