PHP code examples to call Baidu Interface

php call Baidu new translation of the interface code, there is little change in the way call. For details, refer Baidu interface documentation. Translation of value. No more urlencode transformation, we need to verify the signature translation. Specific code as follows:

Language function ($ value, $ from = "Auto", $ to = "Auto") 
{ 
     $ value_code = $ value; // urlencode ($ value); // First the text to be translated be treated urlencode 
     $ appid = " 20151119000000001 "; // your registered key API 
     $ key =" 12345678 "; // key 
     $ salt = rand (1000000000,9999999999); // random number 
     .. $ sign = md5 ($ appid $ value_code $ salt $. key); // signature 
     // generate a URL GET address translation API's 
     $ languageurl = "http://api.fanyi.baidu.com/api/trans/vip/translate?q=$value_code&appid=$appid&salt=$salt&from= $ from & to = $ to & Sign = $ Sign "; 

     $ text = json_decode (LanguageText ($ languageurl)); 
     return $ tEXT-> trans_result; 
} 

function LanguageText ($ url) // obtain the target URL of the print content 
{ 
     IF (!function_exists('file_get_contents')) 
     {
          $file_contents = file_get_contents($url);
     } else {
          $ch = curl_init();
          curl_setopt ($ch, CURLOPT_URL, $url);
          curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
          $file_contents = curl_exec($ch);
          curl_close($ch);
     }
     return $file_contents;
}
// echo language('中国', 'zh', 'en');



$val = $post['val'];
$lan = language($val, 'zh', 'en');
$result = '';
foreach ($lan as $k => $v)
{
     $result .= ucwords($v->dst) ."\n";
}
echo json_encode($result);
exit();

 Original Transfer: http://blog.sina.com.cn/s/blog_6ad624380102w2f2.html

Guess you like

Origin www.cnblogs.com/gyrgyr/p/11199108.html