1

When I send a string to a PHP page it works fine.

The problem is when I use Arabic language like "محمد", I get a result like "؟؟؟؟". Here is my Delphi code:

var
  s: string;
  server: TIdHttp;
begin
  s := 'محمد';
  server := TIdHttp.Create;
  server.Post('http://mywebsite.com/insert_studint.php?name1=' + s);
  server.Free;
end;

My PHP code:

<?php
    $name1=$_post['name1'];
    echo $name1;
?>

How can I encode my request to UTF-8 to get a proper result on my PHP server?

STF
  • 1,485
  • 3
  • 19
  • 36
tamer naffaa
  • 21
  • 1
  • 3
  • https://stackoverflow.com/questions/44214787/get-utf8-string-from-php-page-in-c-sharp-windows-application – Ali Jun 04 '17 at 16:20

1 Answers1

2

That's not how URL string query parameters are handled by the TIdHTTP class. The first parameter of the Post method is just the target URL. The URL string query parameters need to be passed as the second parameter of this method as a stream or a string list collection. You need to specify the request encoding in your code as well and the rest will handle the class internally. Try this instead:

var
  Server: TIdHTTP;
  Params: TStrings;
begin
  Server := TIdHTTP.Create;
  try
    { setup the request charset }
    Server.Request.Charset := 'utf-8';
    { create the name=value parameter collection }
    Params := TStringList.Create;
    try
      { add the name1 parameter (concatenated with its value) }
      Params.Add('name1=محمد');
      { do the request }
      Server.Post('http://mywebsite.com/insert_studint.php', Params);
    finally
      Params.Free;
    end;
  finally
    Server.Free;
  end;
end;
Victoria
  • 7,822
  • 2
  • 21
  • 44