[英]php: file_get_contents encoding problem
我的任務很簡單:向translate.google.com發帖請求並獲取翻譯。 在下面的例子中,我使用“hello”這個詞翻譯成俄語。
header('Content-Type: text/plain; charset=utf-8'); // optional
error_reporting(E_ALL | E_STRICT);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => implode("\r\n", array(
'Content-type: application/x-www-form-urlencoded',
'Accept-Language: en-us,en;q=0.5', // optional
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' // optional
)),
'content' => http_build_query(array(
'prev' => '_t',
'hl' => 'en',
'ie' => 'UTF-8',
'text' => 'hello',
'sl' => 'en',
'tl' => 'ru'
))
)
));
$page = file_get_contents('http://translate.google.com/translate_t', false, $context);
require '../simplehtmldom/simple_html_dom.php';
$dom = str_get_html($page);
$translation = $dom->find('#result_box', 0)->plaintext;
echo $translation;
標記為可選的行是那些沒有輸出相同的行。 但我得到了奇怪的人物......
������
我試過了
echo mb_convert_encoding($translation, 'UTF-8');
但我明白了
ÐÒÉ×ÅÔ
有人知道如何解決這個問題嗎?
更新:
如果它可以幫助CURL導入字符編碼問題 ,請嘗試查看此帖子
你也可以嘗試這個片段(取自php.net)
<?php
function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
return mb_convert_encoding($content, 'UTF-8',
mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}
?>
首先,您的瀏覽器是否設置為UTF-8? 在Firefox中,您可以在View-> Character Encoding中設置文本編碼。 確保選中“Unicode(UTF-8)”。 我還將View-> Character Encoding-> Auto-Detect設置為“Universal”。
其次,您可以嘗試傳遞FILE_TEXT標志,如下所示:
$page = file_get_contents('http://translate.google.com/translate_t', FILE_TEXT, $context);
Accept-Charset並不是那么可選。 你應該在那里指定UTF8。 俄語字符在ISO_8859-1中無效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.