How to do HTTP POST to Utf-8 & # 8594; PHP script & # 8594; mysql

I am using Delphi 7 and ICS components to communicate with php script and insert some data into mysql database ...

How to post data in Unicode using http post?

After using utf8encode from tnt controls I am doing this to post to PHP script

<?php
echo "Note = ". $_POST['note'];

if($_POST['action'] == 'i')
 {
    /*
     *  This code will add new notes to the database
     */
    $sql = "INSERT INTO app_notes VALUES ('', '" . mysql_real_escape_string($_POST['username']) . "', '" . mysql_real_escape_string($_POST['note']) . "', NOW(), '')";
    $result = mysql_query($sql, $link) or die('0 - Ins');
    echo '1 - ' . mysql_insert_id($link);
?>

      

Delphi code:

  data := Format('date=%s&username=%s&password=%s&hash=%s&note=%s&action=%s',
                   [UrlEncode(FormatDateTime('yyyymmddhh:nn',now)),
                    UrlEncode(edtUserName.Text),
                    UrlEncode(getMd51(edtPassword.Text)),
                    UrlEncode(getMd51(dataHash)),UrlEncode(Utf8Encode(memoNote.Text)),'i'
                    ]);

//  try  function StrHtmlEncode (const AStr: String): String; from IdStrings

    HttpCli1.SendStream := TMemoryStream.Create;
    HttpCli1.SendStream.Write(Data[1], Length(Data));
    HttpCli1.SendStream.Seek(0, 0);
    HttpCli1.RcvdStream := TMemoryStream.Create;
    HttpCli1.URL := Trim(ActionURLEdit.Text);
    HttpCli1.PostAsync;

      

But when I post that the unicode value is completely different from the original I see in the Tnt Memo

Is there something I am missing ?!

Also does anyone know how to do this with Indy?

Thank.

+1


source to share


3 answers


Your code example displays data coming from a TNT Unicode control. This value will be of type WideString

, so to get UTF-8 data you must call Utf8Encode

that will return the value AnsiString

. Then call UrlEncode

on that value. Make sure the input type is UrlEncode

AnsiString

. So, something like this:

var
  data, date, username, passhash, datahash, note: AnsiString;

date := FormatDateTime('yyyymmddhh:nn',now);
username := Utf8Encode(edtUserName.Text);
passhash := getMd51(edtPassword.Text);
datahash := getMd51(data);
note := Utf8Encode(memoNote.Text);
data := Format('date=%s&username=%s&password=%s&hash=%s&note=%s&action=%s',
               [UrlEncode(date),
                UrlEncode(username),
                UrlEncode(passhash),
                UrlEncode(datahash),
                UrlEncode(note),
                'i'
               ]);

      

There is no need to UTF-8-encode MD5 values ​​as MD5 string values ​​are hexadecimal. However, you have to double check what your function is getMd51

accepting WideString

. Otherwise, you may lose data before sending it anywhere.

Then you got the problem of getting UTF-8 data in PHP. I expect there is nothing special you need to do there or in MySQL. Whatever you keep, you should come back to the same later. Post this back to your Delphi program and convert the UTF-8 data to WideString

.



In other words, your Unicode data will look different in your database because you store it as UTF-8. In your database, you see data encoded in UTF-8, but in TNT controls, you see normal Unicode characters.

So, for example, if you enter the character "ش" in the edit box, that Unicode character is U + 0634, an Arabic alphabetic character. As UTF-8, this two-byte sequence is 0xD8 0xB4. If you store these bytes in your database and view the raw content of the field, you can see the characters interpreted as if the bytes were in some ANSI encoding. One possible interpretation of these bytes is the two-character sequence "Ø", which is a Latin capital letter o with a stroke followed by a sharp accent.

When you load this string from your database, it is still encoded as UTF-8, the same as when you save it, so you need to decode it. As far as I can tell, neither PHP nor MySQL massages your data, so any UTF-8 character you give them will be returned to you as is. If you are using data in Delphi, call Utf8Decode

that is in addition to the function Utf8Encode

you called earlier. If you're using data in PHP, you might be interested in the PHP function utf8_decode

, although it converts to ISO-8859-1, which doesn't include our Arabic character example. There are already several questions in Stack Overflow related to using UTF-8 in PHP, so I won't try to add them here. For example:

+3


source


Encode UTF-8 data in app / x-www-form-urlencoded. This ensures that the server can read data over the http connection.



+2


source


I would expect (not knowing for sure) that you would have to output them as nouns. #nnnnn (with a number in decimal, not hexadecimal) I guess)

0


source







All Articles