forked from china-liweihong/zhibo_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baiduapi.php
156 lines (140 loc) · 3.45 KB
/
baiduapi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/***************************************************************************
* Copyright (c) 2015 Baidu.com, Inc. All Rights Reserved
*
**************************************************************************/
/**
* @file baidu_transapi.php
* @author mouyantao([email protected])
* @date 2015/06/23 14:32:18
* @brief
*
**/
define("CURL_TIMEOUT", 10);
define("URL", "http://api.fanyi.baidu.com/api/trans/vip/translate");
define("APP_ID", "20161221000034359"); //替换为您的APPID
define("SEC_KEY", "eMgCriOPVJ4DmXMizDKJ");//替换为您的密钥
$content=$_GET['content'];
$from=$_GET['from'];
$to=$_GET['to'];
//翻译入口
function translate($query, $from, $to)
{
$args = array(
'q' => $query,
'appid' => APP_ID,
'salt' => rand(10000,99999),
'from' => $from,
'to' => $to,
);
$args['sign'] = buildSign($query, APP_ID, $args['salt'], SEC_KEY);
$ret = call(URL, $args);
$ret = json_decode($ret, true);
$word = $ret['trans_result'][0];
/* if($from == "th"){
$return = array(
"th" => $word['src'],
"zh" => $word['dst']
);
}else{
$return = array(
"th" => $word['dst'],
"zh" => $word['src']
);
} */
$str = $word['src']."(".$word['dst'].")";
echo $str;
//return $ret;
}
//加密
function buildSign($query, $appID, $salt, $secKey)
{/*{{{*/
$str = $appID . $query . $salt . $secKey;
$ret = md5($str);
return $ret;
}/*}}}*/
//发起网络请求
function call($url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $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 = CURL_TIMEOUT, $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;
}/*}}}*/
$q="中国,你好,我的名字是测试笔记本utf8";
//$from="zh";
//$to="th";
translate($content,$from,$to);
?>