PHP調用有道詞典翻譯API實現通譯功能及代碼

來源:互聯網
上載者:User
PHP調用有道詞典翻譯API實現翻譯功能及代碼
PHP調用有道詞典翻譯API實現翻譯功能及代碼,其中英文可以翻譯,中文不能翻譯是什麼問題啊?急
http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml&version=1.1&q=中文"
------解決方案--------------------
function getTransContent($url) {
// $url = "http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml&version=1.1&q=" .urlencode($this->keyword);
// echo $url;
// $url = urlencode($url);
$value = file_get_contents($url);//echo $value;


// $ch = curl_init();

// curl_setopt($ch, CURLOPT_URL, $url);

// $html = curl_exec($ch);
$ar = simplexml_load_string($value,'SimpleXMLElement',LIBXML_NOCDATA
------解決方案--------------------
LIBXML_NOBLANKS);
//$jsonValue = json_decode ( $value, true );

$errorCode = $ar->errorCode;echo $errorCode.PHP_EOL;
//翻譯成功
// $errorCode = $jsonValue["erroeCode"];
$trans = '';

if (isset($errorCode)) {
switch ($errorCode) {
case 0:
$trans = $ar->translation->paragraph;
break;
case 20:
$trans = '要翻譯的文本過長';
break;
case 30:
$trans = '無法進行有效翻譯';
break;
case 40:
$trans = '不支援的語言類型';
break;
case 50:
$trans = '無效的key';
break;
default:
$trans = '出現異常';
break;
}
}
return $trans;
}
$s=urlencode('中文');
echo getTransContent("http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml&version=1.1&q=$s");

------解決方案--------------------
$c = iconv('gbk', 'utf-8', '中文'); //gbk 一定要轉成 utf-8
$c = urlencode($c); //必須做 url 編碼
$url = "http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=json&version=1.1&q=$c";
//傳回型別 設為 json,這樣處理起來方便些
$s = file_get_contents($url);
print_r(json_decode($s, 1));
Array
(
[translation] => Array
(
[0] => Chinese
)

[basic] => Array
(
[phonetic] => zhōng wén
[explains] => Array
(
[0] => [語] Chinese
)

)

[query] => 中文
[errorCode] => 0
[web] => Array
(
[0] => Array
(
[value] => Array
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.