码迷,mamicode.com
首页 > Web开发 > 详细

php 百度翻译

时间:2017-12-29 21:01:13      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:public   tran   curl   http   this   span   result   top   put   

class fanyi{
    public function fahui($res)
    {
     $url="http://api.fanyi.baidu.com/api/trans/vip/translate";
     $q=urlencode($res);
     $from="auto";
     $to="auto";
     $appid="你的appid";
     $salt= rand(10000,99999);
     $mishi="你的密钥";
     $sign= md5($appid.$res.$salt.$mishi);

     
     $url="http://api.fanyi.baidu.com/api/trans/vip/translate?q=".$q."&from=".$from."&to=".$to."&appid=".$appid."&salt=".$salt."&sign=".$sign;
     $result= json_decode($this->crul($url));
    
     $r= (array)$result->trans_result; 
     
    
     echo "yuan:".$r[0]->src;
     echo "<br>";
     echo "result:".$r[0]->dst;
    }
    
    public function crul($url){
        $ch= curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
        $file_contect=curl_exec($ch);
        curl_close($ch);
        file_put_contents("ccc", $file_contect);
        return $file_contect;
        
    }
}

 

php 百度翻译

标签:public   tran   curl   http   this   span   result   top   put   

原文地址:https://www.cnblogs.com/zywf/p/8146126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!