{ "translation": [{ "translated": [{ "src-tokenized": ["我", " ", "喜", "欢", " ", "你"], "score": -5.687654018402099, "rank": 0, "text": "I like you" }], "translationId": "6c64d73b1174431eb2f743c6eac5983c" }] }
<?php /** * Created by PhpStorm. * User: FZS * Time: 2020/01/06 22:16 */ //---------------------------------- // 译云机器翻译 调用类 //---------------------------------- class freeApi{ private $apiUrl; private $appKey; private $appId; public function __construct($appKey,$appId){ $this->appKey = $appKey; $this->appId = $appId; $this->apiUrl = 'http://api.yeekit.com/dotranslate.php'; } /** * 获取结果 * @return array */ public function getResult(){ $paras = [ 'app_kid' => $this->appKey, 'app_key' => $this->appId, 'from' => 'zh', 'to' => 'en', 'text' => '我喜欢你' ]; return $this->freeApiCurl($this->apiUrl,$paras); } /** * 请求接口返回内容 * @param string $url [请求的URL地址] * @param string $params [请求的参数] * @param int $ipost [是否采用POST形式] * @return string */ public function freeApiCurl($url,$params=false){ $headers = array('Content-Type: application/x-www-form-urlencoded'); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($curl); // 执行操作 if (curl_errno($curl)) { echo 'Errno'.curl_error($curl);//捕抓异常 } curl_close($curl); // 关闭CURL会话 return $result; } }
package main import ( "fmt" "io/ioutil" "log" "net/http" ) const ( APIURL = "http://api.yeekit.com/dotranslate.php" APIKEY = "APP ID" APIID = "密钥" ) func main() { queryUrl := fmt.Sprintf("%s?app_kid=%s&app_key=%s&from=zh&to=en&text=我喜欢你",APIURL,APIKEY,APIID) resp, err := http.Get(queryUrl) if err != nil { log.Println(err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return } fmt.Println(string(body)) }