当前位置:博客首页>>PHP >> 阅读正文

PHP调用谷歌翻译实现翻译功能

作者: 郑晓 分类: PHP 发布于: 2012-05-02 07:51 浏览:6,227 没有评论


<?php
/*
 * PHP调用谷歌翻译
 */

function translate($text,$language='zh-cn|en'){
 if(empty($text))return false;
 @set_time_limit(0);
 $html = "";
 $ch=curl_init("http://google.com/translate_t?langpair=".urlencode($language)."&text=".urlencode($text));
 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 curl_setopt($ch,CURLOPT_HEADER, 0);
 curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    $html=curl_exec($ch);
    if(curl_errno($ch))$html = "";
 curl_close($ch);
 if(!empty($html)){
  $x=explode("</span></span></div></div>",$html);
  $x=explode("onmouseout="this.style.backgroundColor='#fff'">",$x[0]);
  return $x[1];
 }else{
  return false;
 }
}
echo translate('你好');
       

本文采用知识共享署名-非商业性使用 3.0 中国大陆许可协议进行许可,转载时请注明出处及相应链接。

本文永久链接: https://www.zh30.com/php-calls-the-google-translation-translation-function.html

发表评论

change vcode