skip to Main Content

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


  1. Try using curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

    Login or Signup to reply.
  2. Don’t use urlencode() when you’re putting the parameters in an array with CURLOPT_POSTFIELDS. The documentation says:

    If value is an array, the Content-Type header will be set to multipart/form-data.

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search