phpunit和curl错误的charset,无法比较相等的字符串



我有一个带有断言的phpunit测试: $this->assertEquals(json_encode($expected), json_encode($result));

断言失败了,因为字符串的编码不同:(差异实际上是更大的,来自大的JSON对象)

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Wir holen Ihre Pu00e4ckchen im Wunschzeitraum ab.'
+'Wir holen Ihre Pu00c3u00a4ckchen im Wunschzeitraum ab.'

我从卷曲请求中获取实际字符串,预期字符串被定义为PHP文件中的字符串。

我使用以下卷曲代码从服务器检索JSON:

$headers = array(
"GET /HTTP/1.1",
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1",
"Content-type: application/json;charset="utf-8"",
"Accept: application/json",            
"Accept-Language: en-us,en;q=0.5",
"Accept-Encoding: gzip,deflate",
"Accept-Charset: utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 300",      
"Connection: keep-alive");
$ch = curl_init('http://localhost/frontend_test.php/api');
curl_setopt_array($ch, array(
  CURLOPT_POST => TRUE,
  CURLOPT_RETURNTRANSFER => TRUE,
  CURLOPT_HTTPHEADER => $headers,
  CURLOPT_POSTFIELDS => json_encode($some_data),
  CURLOPT_HEADER => 1
));
// Send the request
$response = curl_exec($ch);
list($header, $body) = explode("rnrn", $response, 2);
$result = json_decode($body);

这可能是什么原因?

好的,因此在我的情况下,解决方案很简单:PDO连接使用了错误的字符集。(API从数据库中检索了所讨论的字符串,但未正确编码)愚蠢的错误

最新更新