I have very strange problem with urlencode
and now i explain:
I have a telegram bot the send a message to me with url, so for this reason i want to add urlencode
fot text that i will paste in url.
But if use CURLOPT_POSTFIELDS
i have the strange problem.
The message to send is:
This is an example my friend
But if use urlencode
and CURLOPT_POSTFIELDS
the output is:
This+is+an+example+my+friend
Now i show the full code:
$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);
}
I hope someone can help me…
thanks a lot and sorry for my english
2
Answers
Try using
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
Don’t use
urlencode()
when you’re putting the parameters in an array withCURLOPT_POSTFIELDS
. The documentation says:This format doesn’t require URL-encoding, so the server won’t automatically decode it. As a result, the characters used in the encoding (space is encoded as
+
, other special characters use%
followed by hex codes) will be treated literally.