我一直在尋找和嘗試幾個小時,似乎無法找到任何能夠真正解決我的問題的東西。 我打電話給一個PHP函數,它使用Google翻譯API抓取內容,並傳遞一個要翻譯的字符串。 有很多情況下,編碼受到影響,但我已經完成了這一點,它仍然工作得很好,據我記憶。PHP的編碼問題
下面是調用該函數的代碼:
$name = utf8_encode(mt($name));
而這裏的實際功能:
function mt($text) {
$apiKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source=en&target=es';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($handle);
echo curl_error($handle);
$responseDecoded = json_decode($response, true);
$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); //Fetch the HTTP response code
curl_close($handle);
if($responseCode != 200) {
$resultxt = 'failed!';
return $resultxt;
}
else {
$resultxt = $responseDecoded['data']['translations'][0]['translatedText'];
return utf8_decode($resultxt); //return($resultxt) won't work either
}
}
我到底是什麼了越來越爲任何加重字符亂碼字符,如顧Ã del desarrollador de XML 我試過編碼/解碼的所有組合我只是無法得到它的工作...
你在哪裏看到亂碼?在一個HTML文件中? – Phil
我正在用從該函數返回的字符串記錄一個XML文件。我多次調用該函數,用於我需要翻譯的XML文件的每個元素。我正在使用SimpleXML - '$ xml-> asXML($ xmlFile)' –
我想出了(部分)。我正在處理上述的XML文件,它需要@gonzalon建議的編碼聲明,並且必須使用以下代碼才能使其正確顯示: '$ resultxt = $ responseDecoded ['data'] ['translations' ] [0] [ 'translatedText']; $ str = html_entity_decode($ resultxt); $ utf8 = utf8_decode($ str); return $ utf8;' –