PHP calls Youdao dictionary translation API to implement translation functions and code

Source: Internet
Author: User
Tags unsupported
PHP calls Youdao dictionary translation API to implement translation functions and code, where English can be translated and Chinese cannot be translated. what is the problem? Urgent
Http://fanyi.youdao.com/openapi.do? Keyfrom = cxvsdffd33 & key = 1310976914 & type = data & doctype = xml & version = 1.1 & q = Chinese"


Reply to discussion (solution)

Encode keywords with urlencode.

You can try using the url provided by me. q is followed by a Chinese parameter.

I have tried encoding.

We recommend that you paste the code so that others can directly test the code.

Http://fanyi.youdao.com/openapi.do? Keyfrom = cxvsdffd33 & key = 1310976914 & type = data & doctype = xml & version = 1.1 & q = you can add a parameter after q to try it in Chinese and English, respectively, just look at the returned value. In Google's browser, both Chinese and English can be used. in Firefox, Chinese cannot be used. why ????

Public 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 ); $ jsonValue = json_decode ($ value, true); // Translation successful $ errorCode = $ jsonValue ["erroeCode"]; $ trans = ''; if (isset ($ errorCode )) {switch ($ errorCode) {case 0: $ trans = $ jsonValue ["translation"] ['0']; break; case 20: $ trans = 'long text to be translated '; break; case 30: $ trans = 'no valid translations allowed'; break; case 40: $ trans = 'unsupported language type'; break; case 50: $ trans = 'invalid key'; break; default: $ trans = 'exception '; break;} return $ trans ;}}

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_noblks); // $ jsonValue = json_decode ($ value, true); $ errorCode = $ ar-> errorCode; echo $ errorCode. PHP_EOL; // Translation successful // $ errorCode = $ jsonValue ["erroeCode"]; $ trans = ''; if (isset ($ errorCode) {switch ($ errorCode) {case 0: $ trans = $ ar-> translation-> paragraph; break; case 20: $ trans = 'long text to translate '; break; case 30: $ trans = 'invalid translators '; break; case 40: $ trans = 'unsupported language type'; break; case 50: $ trans = 'invalid key'; break; default: $ trans = 'exception occurred '; break ;}return $ trans ;}$ s = urlencode ('Chinese '); 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', 'Chinese'); // gbk must be converted to UTF-8 $ c = urlencode ($ c ); // url encoding required $ url = "http://fanyi.youdao.com/openapi.do? Keyfrom = cxvsdffd33 & key = 1310976914 & type = data & doctype = json & version = 1.1 & q = $ c "; // set the return type to json, this makes it easier to process $ 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] => [language] Chinese )) [query] => Chinese [errorCode] => 0 [web] => Array ([0] => Array ([value] => Array ([0] => Chinese [1] => facebook [2] => English) [key] => Chinese) [1] => Array ([value] => Array ([0] => Chinese school [1] => Schools-Chinese [2] => Chinese Language School) [key] => Chinese School) [2] => Array ([value] => Array ([0] => fbook [1] => facebook [2] => face) [key] => Chinese logon )))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.