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

Source: Internet
Author: User
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

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.