如何在Utf-8中进行HTTP POST?> php脚本 - > mysql [英] How to do HTTP POST in Utf-8 -> php script -> mysql

查看:238
本文介绍了如何在Utf-8中进行HTTP POST?> php脚本 - > mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi 7和ICS组件与php脚本进行通信,并在mysql数据库中插入一些数据...



如何使用http post发布unicode数据?



在使用tnt控件中的utf8encode之后,我正在将其发布到PHP脚本

 <?php 
echoNote =。 $ _POST ['note'];

if($ _ POST ['action'] =='i')
{
/ *
*此代码将向数据库添加新笔记
* /
$ 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代码:

  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函数StrHtmlEncode(const AStr:String):String;来自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;

但是当我发布的unicode值是完全不同的,原来我在Tnt Memo中看到

有没有我缺少的东西?



还有人知道如何使用Indy吗?



谢谢。

解决方案

您的示例代码显示了来自TNT Unicode控件的数据。该值将具有类型 WideString ,因此要获取UTF-8数据,您应该调用 Utf8Encode ,这将返回 AnsiString 值。然后在该值上调用 UrlEncode 。确保 UrlEncode 的输入类型是 AnsiString 。所以,这样的东西:

  var 
数据,日期,用户名,passhash,数据注释,注释: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'
]) ;

由于MD5字符串值只是十六进制,所以不需要对MD5值进行UTF-8编码人物。但是,您应该仔细检查您的 getMd51 函数是否接受 WideString 。否则,您可能会在任何地方发送数据之前丢失数据。



接下来,您有在PHP中接收UTF-8数据的问题。我期望在MySQL或MySQL中不需要做任何事情。无论你存储什么,你应该在以后恢复相同。将其发送回您的Delphi程序,并将UTF-8数据解码回 WideString



在其他您的Unicode数据在数据库中看起来不同,因为您将其存储为UTF-8。在您的数据库中,您将看到UTF-8编码的数据,但在TNT控件中,您会看到常规的Unicode字符。



所以,如果您在编辑框中输入字符ش,那就是Unicode字符U + 0634,阿拉伯字母光泽。作为UTF-8,这是两个字节的序列0xD8 0xB4。如果您将这些字节存储在数据库中,然后查看字段的原始内容,则可能会看到字符被解释为似乎这些字节在某些ANSI编码中。这些字节的一个可能的解释是两个字符的序列Ø,这是拉丁字母o,其中笔画后面紧跟着重音。



当你将该字符串加载到数据库中,它仍然被编码为UTF-8,就像存储它一样,因此您需要对其进行解码。据我所知,PHP和MySQL都不会对您的数据进行任何按摩,所以无论您提供的UTF-8字符将如何返回给您。如果您使用Delphi中的数据,请调用 Utf8Decode ,这是您调用的 Utf8Encode 函数的补充先前。如果您使用PHP中的数据,那么您可能对PHP的 utf8_decode 函数感兴趣,尽管转换为ISO-8859-1,但不包括我们的示例阿拉伯语字符。堆栈溢出已经有一些与在PHP中使用UTF-8有关的问题,所以我不会尝试添加到这里。例如:




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

How to post unicode data using http post ?

After using utf8encode from tnt controls I am doing it 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 unicode value is totally different then original one that I see in Tnt Memo

Is there something I am missing ?!

Also anybody knows how to do this with Indy?

Thanks.

解决方案

Your example code shows your data coming from a TNT Unicode control. That value will have type WideString, so to get UTF-8 data, you should call Utf8Encode, which will return an AnsiString value. Then call UrlEncode on that value. Make sure UrlEncode's input type is 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 should be no need to UTF-8-encode the MD5 values since MD5 string values are just hexadecimal characters. However, you should double-check that your getMd51 function accepts WideString. Otherwise, you may be losing data before you ever send it anywhere.

Next, you have the issue of receiving UTF-8 data in PHP. I expect there's nothing special you need to do there or in MySQL. Whatever you store, you should get back identically later. Send that back to your Delphi program, and decode the UTF-8 data back into a WideString.

In other words, your Unicode data will look different in your database because you're storing it as UTF-8. In your database, you're seeing UTF-8-encoded data, but in your TNT controls, you're seeing the regular Unicode characters.

So, for instance, if you type the character "ش" into your edit box, that's Unicode character U+0634, Arabic letter sheen. As UTF-8, that's the two-byte sequence 0xD8 0xB4. If you store those bytes in your database, and then view the raw contents of the field, you may see characters interpreted as though those bytes are in some ANSI encoding. One possible interpretation of those bytes is as the two-character sequence "Ø´", which is the Latin capital letter o with stroke followed by an acute accent.

When you load that string back out of your database, it's still encoded as UTF-8, just as it was when you stored it, so you will need to decode it. As far as I can tell, neither PHP nor MySQL does any massaging of your data, so whatever UTF-8 character you give them will be returned to you as-is. If you are using the data in Delphi, then call Utf8Decode, which is the complement to the Utf8Encode function that you called previously. If you are using the data in PHP, then you might be interested in PHP's utf8_decode function, although that converts to ISO-8859-1, which doesn't include our example Arabic character. Stack Overflow already has a few questions related to using UTF-8 in PHP, so I won't attempt to add to them here. For example:

这篇关于如何在Utf-8中进行HTTP POST?&gt; php脚本 - &gt; mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆