我有一個PHPUnit的測試與斷言: $this->assertEquals(json_encode($expected), json_encode($result));
PHPUnit的和捲曲錯誤的字符集,不能比較平等的字符串
因爲琴絃在它們編碼不同的斷言失敗:(的差異實際上是較大的,從一個大的JSON對象)
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Wir holen Ihre P\u00e4ckchen im Wunschzeitraum ab.'
+'Wir holen Ihre P\u00c3\u00a4ckchen im Wunschzeitraum ab.'
我從一個curl請求中獲得實際的字符串,期望的字符串在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("\r\n\r\n", $response, 2);
$result = json_decode($body);
什麼可能是這個原因嗎?
該文件編碼爲utf-8,當我檢查一個ajax調用該api它說這是utf-8 –
什麼是你使用的硬編碼的PHP字符串?因爲預期的字符串'Wir holen IhrePüuckeckchenim Wunschzeitraum ab',所以你把它存儲爲UTF-8。更小,它可能不會被正確編碼爲UTF-8。你使用UTF8_encode()嗎? http://www.php.net/manual/en/function.utf8-encode.php –
php字符串在一個大數組內,這裏就是它在php文件中的部分:array(「description」=> 「Wir holen IhrePäckchenim Wunschzeitraum ab。」)我還嘗試使用utf8_encode和iconv將json_encodes的結果編碼爲utf-8。不會改變任何東西。它可以與utf-8和utf-16有關嗎?在我看來,較大的版本使用兩個字節作爲「ä」字符,而另一個字節可能只使用一個字節? –