PHP call Youdao dictionary translation API to implement the translation function and code

Source: Internet
Author: User
Tags unsupported urlencode
PHP call Youdao dictionary translation API to implement the translation function and code, in which English can be translated, Chinese can not translate what is the problem ah? Tokyu
http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml& Version=1.1&q= Chinese "


Reply to discussion (solution)

Encode the keyword with urlencode.

You can directly have the URL address I provided to try, Q back with good Chinese parameters

I've tried the code.

Suggest or post code, so that others can directly test.

http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data&doctype=xml& Version=1.1&q= you directly after the Q to add a parameter, respectively, in Chinese and English try to see the return value on the line. There are also in the Google browser in English and Chinese can, in the Firefox browser, English can not, this is why????

Public Function Gettranscontent ($url) {//$url = "http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=131     0976914&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);        Successful translation $errorCode = $jsonValue ["Erroecode"];                $trans = ";        if (Isset ($errorCode)) {switch ($errorCode) {case 0: $trans = $jsonValue ["translation"] [' 0 '];        Break        Case: $trans = ' The text to be translated is too long ';        Break        Case: $trans = ' Unable to translate effectively ';        Break        Case: $trans = ' unsupported language type ';        Break        Case: $trans = ' Invalid key ';        Break        Default: $trans = ' exception occurred '; 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_noblanks);        $jsonValue = Json_decode ($value, true); $errorCode = $ar->errorcode;echo $errorCode. Php_eol;        Translation Success//$errorCode = $jsonValue ["Erroecode"];                $trans = ";        if (Isset ($errorCode)) {switch ($errorCode) {case 0: $trans = $ar->translation->paragraph;        Break        Case: $trans = ' The text to be translated is too long ';        Break        Case: $trans = ' Unable to translate effectively ';        Break Case 40:       $trans = ' unsupported language type ';        Break        Case: $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 turned into utf-8$c = UrlEncode ($c); URL encoding must be done = "http://fanyi.youdao.com/openapi.do?keyfrom=cxvsdffd33&key=1310976914&type=data& doctype=json&version=1.1&q= $c ";//The return type is set to JSON, which makes it easier to handle $s = file_get_contents ($url);p Rint_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] = A  Rray ([value] = = Array ([0] = =                        Chinese [1] = = Facebook [2] = 中文版 ) [Key] = Chinese) [1] = = Array ([valu  E] = = 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 login))
  • Related Article

    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.