【问题标题】:C# - PHP character encoding mismatchC# - PHP 字符编码不匹配
【发布时间】:2016-05-31 12:26:40
【问题描述】:

我们正在开发一个严重依赖于 C# 和 PHP 之间稳定通信和数据传输的应用程序。


计划:

在 C# 中创建一个桌面应用程序,我们可以在其中编辑通过向 PHP 服务器发出 api 请求返回的值。


问题

当使用 C# RestClient 类向 PHP 发送包含特殊字符的值时,例如:é,我们发现在到达 PHP api 时,值已从例如:é 更改为 @987654324 @。

在搜索一些相关信息后,我们发现这与字符编码有关,据我们所知,PHP api 使用utf-8,C# 使用utf-8


守则


发送和创建请求的 C# 部分是:

string tmp = JsonConvert.SerializeObject(deelname);
client = new RestClient((local) ? "http://rotserver" : "https://registrations.roundtexel.com/");
var request = new RestRequest((local) ? "?action=deelname" : "api/bewerk", Method.POST);
request.AddParameter("username", (local) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(User)) : User);
request.AddParameter("type", (local) ? Convert.ToBase64String(Encoding.UTF8.GetBytes("deelname")) : "deelname");
request.AddParameter("inschrijf_id", (local) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(inschrijf_id)) : inschrijf_id);
request.AddParameter("stuurman_id", (local) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(stuurman_id)) : stuurman_id);
request.AddParameter("bemanning_id", (local) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(bemanning_id)) : bemanning_id);
request.AddParameter("voertuig_id", (local) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(voertuig_id)) : voertuig_id);
request.AddParameter("boot_id", (local) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(boot_id)) : boot_id);
request.AddParameter("content", (local) ? Convert.ToBase64String(Encoding.UTF8.GetBytes(tmp)) : tmp);
IRestResponse response = client.Execute(request);

PHP/api部分代码:

public function __construct() {
    $this->db = new PDO('mysql:host=localhost;dbname=xxxxxx', 'xxxxxx', 'xxxxxx');
    $this->db->exec("SET names utf8;");
}

public function execute(){
    $content = json_decode($_POST['content']);
    #Stuurman
    $stuurman_id = $_POST['stuurman_id'];
    $stuurman = $content->deelnemerStuurman;
    $stmnt = $this->db->prepare("UPDATE `~deelnemer` SET voornaam=?, tussenvoegsel=?, achternaam=?, adres=?, huisnummer=?, toevoeging=?, postcode=?, woonplaats=?, provincie=?, geslacht=?, geboortedatum=?, nationaliteit=(SELECT id FROM `~nationaliteiten` WHERE nationaliteit=?), licentie=1, licentie_nummer=?, telefoonnummer=?, mobielenummer=?, noodnummer=?, email=?, afbeeldingsnaam=?, whatsapp_registratie=? WHERE id=?");
    $stmnt->execute(Array($stuurman->voornaam, $stuurman->tussenvoegsel, $stuurman->achternaam, $stuurman->adres, $stuurman->huisnummer, $stuurman->toevoeging,$stuurman->postcode,$stuurman->woonplaats, $stuurman->provincie, $stuurman->geslacht, $stuurman->geboortedatum,$stuurman->land, $stuurman->licentie_nummer, $stuurman->telefoonnummer, $stuurman->mobielenummer, $stuurman->noodnummer, $stuurman->email, $stuurman->afbeeldingsnaam, $stuurman->whatsapp_registratie, $stuurman_id));
    var_dump($stuurman->voornaam);
    $tmp = "{status: 'success'}";
    return $tmp;
}

由于var_dump,我们发现名称:André已更改为:André

我希望任何人都可以帮助我们解决这个问题, 亲切的问候, 杰弗里

【问题讨论】:

    标签: c# php json post character-encoding


    【解决方案1】:

    您可以尝试对字符串进行 html 转义,以便将 é 更改为 é或é在您发送之前。

    在c#中使用HttpUtility.HtmlEncode,在php中使用html_entity_decode

    【讨论】:

    • 既然你将字符串编码为 url 编码,你应该在 php 中使用 urldecode($string)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多