Search code examples
phpcurlnon-english

CURL changes non-english characters when opening url


I'm trying to open an url with curl end echo the text in that page. The url is sends parameters to the python script and it creates a file according to the parameters. So, i'm sending parameters to wordpress function via ajax, this parts works, it gets parameters without any error, then i'm creating url with this and using curlopt_url to open it in backend. Then echo response to send data to ajax.

  function my_function() {
  $param1 = $_REQUEST['p1'];
  $param2 = $_REQUEST['p2'];
  $url = "http://localhost:1234/handle?a=$param1&b=$param2";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_URL, $url);
  $store = curl_exec ($ch);
  echo substr($store, 1);
  curl_close ($ch); exit();

Assume that p2 contains non-english characters like ş,ğ,ı etc.. The function gets it correctly (I can see it when i echo $url instead of response), but in CURLOPT_URL part they are changing. Additionally, when i open url form browser, it works fine.


Solution

  • From OP success comments I am adding my solution as a answer will help for future visitors:

    You need to use urlencode for non english characters in url.

    $param2 = urlencode($_REQUEST['p2']);
    

    Side note:

    Instead of using encoding for full url use on specific field or param but sometimes you need to encode full url in special characters case.