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? Rush http://fanyi.youdao.com/openapi.do? Keyfrom = cxvsdffd33 & amp; key = 131 PHP call Youdao dictionary translation API to implement translation functions and code
PHP calls the Youdao dictionary translation API to implement translation functions and code. 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"
------ Solution --------------------
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
------ Solution --------------------
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 be translated ';
Break;
Case 30:
$ Trans = 'invalid translations ';
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 ");
------ Solution --------------------
$ C = iconv ('gbk', 'utf-8', 'Chinese'); // Convert gbk 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 to facilitate processing.
$ 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