php之翻译接口
试过很多接口,要么付费要么有限制,下面这个没有任何限制随便用:
<?php
set_time_limit(0);
ini_set ('memory_limit', '128M');
header("Content-Type: text/html;charset=utf-8");
class Translate {
ini_set ('memory_limit', '128M');
header("Content-Type: text/html;charset=utf-8");
class Translate {
static $Lang = Array (
'auto' => '自动检测',
'ara' => '阿拉伯语',
'de' => '德语',
'ru' => '俄语',
'fra' => '法语',
'kor' => '韩语',
'nl' => '荷兰语',
'pt' => '葡萄牙语',
'jp' => '日语',
'th' => '泰语',
'wyw' => '文言文',
'spa' => '西班牙语',
'el' => '希腊语',
'it' => '意大利语',
'en' => '英语',
'yue' => '粤语',
'zh' => '中文'
);
static function getLang() {
return self::$Lang;
}
static function exec($text, $from = 'zh', $to = 'en')
{
$url = "http://fanyi.baidu.com/v2transapi";;
$data = array (
'from' => $from,
'to' => $to,
'query' => $text
);
$data = http_build_query ( $data );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_REFERER, "http://fanyi.baidu.com" );
curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0' );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );
$result = curl_exec ( $ch );
curl_close ( $ch );
'auto' => '自动检测',
'ara' => '阿拉伯语',
'de' => '德语',
'ru' => '俄语',
'fra' => '法语',
'kor' => '韩语',
'nl' => '荷兰语',
'pt' => '葡萄牙语',
'jp' => '日语',
'th' => '泰语',
'wyw' => '文言文',
'spa' => '西班牙语',
'el' => '希腊语',
'it' => '意大利语',
'en' => '英语',
'yue' => '粤语',
'zh' => '中文'
);
static function getLang() {
return self::$Lang;
}
static function exec($text, $from = 'zh', $to = 'en')
{
$url = "http://fanyi.baidu.com/v2transapi";;
$data = array (
'from' => $from,
'to' => $to,
'query' => $text
);
$data = http_build_query ( $data );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_REFERER, "http://fanyi.baidu.com" );
curl_setopt ( $ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:37.0) Gecko/20100101 Firefox/37.0' );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_TIMEOUT, 10 );
$result = curl_exec ( $ch );
curl_close ( $ch );
$result = json_decode($result, true);
//var_dump($result);
// 出错状态码 999
if (isset($result['error'])) {
return 'xerror';
}
return $result ['trans_result'] ['data'];
}
}
function db_connect()
{
$con = mysql_connect(HOST,USER,PWD);
mysql_select_db(DB);
mysql_query('set names utf8',$con);
return $con;
}
function fun()//翻译urlcontent
{
$content = "如果有一天";
$data = (Translate::exec ($content));
if($data == 'xerror')
{
echo 'error'.$arrry_xt[$k]['id'];
exit;
}
$str = '';
foreach ($data as $key => $value)
{
foreach ($value as $key1 => $value1)
{
if($key1 == 'dst')
$str .= $value1;
}
}
//var_dump($result);
// 出错状态码 999
if (isset($result['error'])) {
return 'xerror';
}
return $result ['trans_result'] ['data'];
}
}
function db_connect()
{
$con = mysql_connect(HOST,USER,PWD);
mysql_select_db(DB);
mysql_query('set names utf8',$con);
return $con;
}
function fun()//翻译urlcontent
{
$content = "如果有一天";
$data = (Translate::exec ($content));
if($data == 'xerror')
{
echo 'error'.$arrry_xt[$k]['id'];
exit;
}
$str = '';
foreach ($data as $key => $value)
{
foreach ($value as $key1 => $value1)
{
if($key1 == 'dst')
$str .= $value1;
}
}
var_dump($str);
}
fun();exit;
}
fun();exit;