C#-PHP字符编码不匹配
我们正在开发一个非常依赖于C#和PHP之间稳定通信和数据传输的应用程序C#-PHP字符编码不匹配,c#,php,json,post,character-encoding,C#,Php,Json,Post,Character Encoding,我们正在开发一个非常依赖于C#和PHP之间稳定通信和数据传输的应用程序 计划: 在C#中创建一个桌面应用程序,我们可以编辑通过向PHP服务器发出api请求返回的值 问题 当使用C#RestClient类将包含特殊字符的值(例如:ee发送到PHP时,我们发现在到达PHP api时,该值从例如:ee更改为Ã 在搜索一些相关信息后,我们发现这与字符编码有关,据我们所知,PHP api使用utf-8,而C#使用utf-8 代码 发送和创建请求的C#部分是: string tmp = JsonCo
计划: 在C#中创建一个桌面应用程序,我们可以编辑通过向PHP服务器发出api请求返回的值
问题 当使用C#
RestClient
类将包含特殊字符的值(例如:ee
发送到PHP时,我们发现在到达PHP api时,该值从例如:ee
更改为Ã
在搜索一些相关信息后,我们发现这与字符编码有关,据我们所知,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Ã
我希望任何人都能帮助我们,
亲切问候,,
Jeffrey您可以尝试对字符串进行html转义,以便将é更改为é;或é;在你发送之前
在c#中使用HttpUtility.HtmlEncode,在php中使用html_entity_decode因为您将字符串编码为url编码,所以应该在php中使用urldecode($string)