PHP calls Youdao translation API to translate text online

» network technique » PHP calls Youdao translation API to translate text online

Register Youdao Zhiyun and call Youdao translation resources online



You are wise and wise

The online search resources for Youdao translation are all Youdao get's OpenAPI, but it is said to be invalid. However, after the cloud drop test, it can still be used, but it can not be used in large quantities. Therefore, Youdao Zhiyun is registered here.

Youdao Zhiyun has four products, all of which are core products. Yunluo uses the translation function. Youdao, a newly registered user, is kind enough to give 100 yuan. In view of the relatively low cost of translation function, it can still be used for a period of time.

Use code

Because Youdao Zhiyun officially has phpdemo, you can directly change the demo and use it directly

 //Youdao translation function ydtranslate($query){ $app_ key = '************';// Register Youdao Zhiyun to obtain $sec_ key = '********************';// Register Youdao Zhiyun to obtain $api_ url = ' ';// You can use HTTPS $args = array( 'q' => $query, 'appKey' => $app_ key, 'salt' => rand(10000,99999), 'from' => 'AUTO', 'to' => 'zh-CHS', ); $args['sign'] = buildSign($app_ key, $query, $args['salt'], $sec_ key); $ret = call($api_ url, $args); //echo $ret; $ret = json_ decode($ret, true)['translation'][0]; return $ret; } //Encryption function buildSign($appKey, $query, $salt, $secKey){ $str = $appKey . $query . $salt . $secKey; $ret = md5($str); return $ret; } //Initiate network request function call($url, $args=null, $method="post", $testflag = 0, $timeout = 20, $headers=array()){ $ret = false; $i = 0; while($ret === false){ if($i > 1) break; if($i > 0){ sleep(1); } $ret = callOnce($url, $args, $method, false, $timeout, $headers); $i++; } return $ret; } function callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = 20, $headers=array()){ $ch = curl_ init(); if($method == "post"){ $data = convert($args); curl_ setopt($ch, CURLOPT_ POSTFIELDS, $data); curl_ setopt($ch, CURLOPT_ POST, 1); }else{ $data = convert($args); if($data){ if(stripos($url, "?") >  0){ $url .= "&$data"; }else{ $url .= "?$ data"; } } } curl_ setopt($ch, CURLOPT_ URL, $url); curl_ setopt($ch, CURLOPT_ TIMEOUT, $timeout); curl_ setopt($ch, CURLOPT_ RETURNTRANSFER, 1); if(! empty($headers)){ curl_ setopt($ch, CURLOPT_ HTTPHEADER, $headers); } if($withCookie){ curl_ setopt($ch, CURLOPT_ COOKIEJAR, $_ COOKIE); } $r = curl_ exec($ch); curl_ close($ch); return $r; } function convert(&$args){ $data = ''; if (is_ array($args)){ foreach ($args as $key=>$val){ if (is_ array($val)){ foreach ($val as $k=>$v){ $data .= $key.'['.$k.']='.rawurlencode($v).'&'; } }else{ $data .="$key=".rawurlencode($val)."&"; } } return trim($data, "&"); } return $args; }

It's easy to use

 echo ydtranslate('I go to school');

Comparison with Google translation

Here is just a paragraph of the content I'm translating. Take a look at it.


After using Youdao translation, I looked at Baidu translation and found that the PHP demo of Baidu translation is almost the same as that of Youdao translation. It can be said that, except for the different API calls, they are basically the same.

Related links

You are wise and wise



Your email address will not be disclosed. Required item used * tagging

2 replies to "PHP calls Youdao translation API to translate text online"