如题,稍微研究了一下。写了一个小例子。仅供学习!
<?php function curlRequest($url, $postData = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); if ($postData) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); } $curlResponse = curl_exec($ch); $curlErrno = curl_errno($ch); if ($curlErrno) { $curlError = curl_error($ch); throw new Exception($curlError); } curl_close($ch); return $curlResponse; } function translate($src_text, $fromLanguage, $toLanguage, $GET = false) { $appId = "your appid"; $appSecret = "your secret"; try { $translateUrl = "http://api.fanyi.baidu.com/api/trans/vip/translate"; $params = array( 'q' => $src_text, 'from' => urlencode($fromLanguage) , 'to' => urlencode($toLanguage) , 'appid' => $appId, 'salt' => time() , ); $params['sign'] = md5($params['appid'] . $params['q'] . $params['salt'] . $appSecret); if ($GET) { $translateUrl .= '?' . http_build_query($params); $curlResponse = curlRequest($translateUrl); } else { $curlResponse = curlRequest($translateUrl, $params); } $re = json_decode($curlResponse); $translated = array(); if (isset($re -> error_code)) { $translated['err'] = $re -> error_msg . '(' . $re -> error_code . ')'; switch ($re -> error_code) { case '52001': $translated['err'] .= '[Time Out]'; break; case '52002': $translated['err'] .= '[The translator system error, try later]'; break; case '52003': $translated['err'] .= '[Unauthorized, please check your APP ID and APP SECRET]'; break; } } else { $translated['trans_result'] = array(); foreach ($re -> trans_result as $trans_result) { $translated['trans_result'][] = $trans_result -> dst; } } unset($curlResponse); return $translated; } catch(Exception $e) { $translated['err'] = "Exception: " . $e -> getMessage(); return $translated; } } /* echo 'translate<br/>'; $en_result = translate('我们不仅要做中国互联网有史以来最大的deal,我们更要做中国互联网最成功的deal!这是我们的决心!','zh','en'); $en_result = $en_result['trans_result'][0]; echo '<br/>'; print_r (translate($en_result,'en','zh'));*/ $en_string = 'Six million twenty-one thousand six hundred and two Dispatch of sina science and Technology Beijing time on July 26, the morning news, Amazon announced on Monday, the company has received permission of British civil aviation bureau and other organizations, to be in the country testing UAV delivery business. "Supported by the UK Civil Aviation Authority, the cross department team has allowed Amazon explore three key innovations: tested in the rural and suburban areas beyond line of sight range UAV operations; by the performance of sensor testing to ensure that the UAV can recognize and avoid the obstacles; let a operation personnel to operate at the same time and highly automated drones." Amazon said in a statement. This is in order to expand the Amazon Air Prime UAV distribution business, the business hopes in the next 30 minutes after the user orders, through the UAV delivery. Amazon took the lead in 2014 to the U.S. regulators to submit regulatory approval of similar testing activities. In fact, not only will the Amazon be able to benefit from these tests, the same is true for the British government. "We want to integrate the UAV safely in the whole air system, so as to promote the development and innovation of UAV technology." British civil aviation authority policy director Tim Johnson (Johnson Tim) said, Amazon\'s these tests will provide information for the implementation of our policies and the future." In addition to Amazon, FedEx and UPS for UAV delivery interest. (Ding Hong) '; print_r (translate($en_string,'zh','en')); ?>
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2305
- 用户1336
- 访客11446271
每日一句
Don't focus on why it can't work. Trust the reason why it can.
忘掉所有"不可能"的借口,坚持那一个"可能"的理由。
忘掉所有"不可能"的借口,坚持那一个"可能"的理由。
解决安卓运行错误Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug
Swift标准类库,相当于一些C ++里的STL类库
软件测试教程:如何执行测试
Java中的(耦合)控制反转
win10利用winsat disk测试磁盘读写速度
Android布局嵌套太多层会Crash
不要错过的9.0高分催泪国漫《白蛇:缘起》
如何精简化Xposed
Thinkpad X1隐士 风扇声音过大的解决方法
vs2017编译和使用libcurl库
Thinkpad x1 Extreme黑苹果10.14.5安装完成
Xposed框架的使用
虚拟内存现在还有用吗?
新会员