php - 通过 PHP Curl 进行谷歌翻译
问题描述
我正在尝试使用 Google URL 来获取从俄语或中文或许多其他语言到英语的翻译,它在 CURL 代码中不起作用,当我在邮递员应用程序中使用相同的 URL 时它起作用。我已经尝试了很多东西,也将代码从邮递员导入到 PHP 文件,但仍然是同样的问题。也(utf-8 解码)工作有点但不完全。为 2 种不同语言添加了 2 条评论尝试它们,你可以看到错误
<?php
header('Content-Type: application/json; charset=utf-8');
$dtext = "説明が丁寧、かつ、事例があり、理解しやすいから。";
// the next comment text work in $dtext
// Boa explicação do instrutor sobre os conteúdos do curso, mas as legendas não estão muito boas, tradução está um pouco mal feita.
// Бодро рассказывает и в то же время все понятно, в общем привлекает тем что это не нудно. //this also not works
$text = str_replace(" ","%20",$dtext);
$host = "https://translate.google.com/translate_a/single?client=webapp&sl=auto&tl=en&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=gt&pc=1&otf=1&ssel=0&tsel=0&kc=1&tk=&q={$text}";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $host,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Cookie: NID=190=NY1ox5yIwHWgl-YC23LlJa8mn9_tWoiLRHJGpd8-RMEJsnh-jrF_cOvMEWqSSsR0J7WSrvhXF-_QqJpJ1s75Ymc76YSqXjS9NxXXnQKSDPmVySE0zNlzrVLQqK3IrmTa-et4Bu-8peiwE9jGnv4QFFjgGuxD5E0Mwbe0bzCvLiU",
"Host: translate.google.com",
"Postman-Token: b8b0ae52-b3c2-479e-9c4d-7e73e0540fb8,b70b881c-dcd6-4d23-a9f3-0bd7eeff91e6",
"User-Agent: PostmanRuntime/7.19.0",
"cache-control: no-cache"
),
));
$response = utf8_decode( curl_exec($curl));
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
解决方案
根据您的代码产生的错误,问题在于缺少 SSL 身份验证。我在您的代码中观察到的错误是:
cURL 错误 #:SSL 证书问题:无法获取本地颁发者证书
在写完初始答案后看到您的评论,答案要简单得多 - 您需要urlencode
翻译您要翻译的短语。
/* Use an non-SSL endpoint to eliminate Certificate errors - Not the greatest solution IMO! */
$host = "http://translate.google.com/translate_a/single?client=webapp&sl=auto&tl=en&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=gt&pc=1&otf=1&ssel=0&tsel=0&kc=1&tk=&q=".urlencode( $dtext );
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $host,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Cookie: NID=190=NY1ox5yIwHWgl-YC23LlJa8mn9_tWoiLRHJGpd8-RMEJsnh-jrF_cOvMEWqSSsR0J7WSrvhXF-_QqJpJ1s75Ymc76YSqXjS9NxXXnQKSDPmVySE0zNlzrVLQqK3IrmTa-et4Bu-8peiwE9jGnv4QFFjgGuxD5E0Mwbe0bzCvLiU",
"Host: translate.google.com",
"Postman-Token: b8b0ae52-b3c2-479e-9c4d-7e73e0540fb8,b70b881c-dcd6-4d23-a9f3-0bd7eeff91e6",
"User-Agent: PostmanRuntime/7.19.0",
"cache-control: no-cache"
),
));
$response = utf8_decode( curl_exec( $curl ) );
$err = curl_error( $curl );
curl_close( $curl );
if ( $err ) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
产生:
[[["Because explanation is polite and there are examples, it is easy to understand.","????????????????????????",null,null,3,null,null,null,[[["ad1cd15d6208d1d2821f97fa1c973637","ja_en_2019q2.md"]
]
]
]
,[null,null,null,"Setsumei ga teinei, katsu, jirei ga ari, rikai shi yasuikara."]
]
,null,"ja",null,null,[["????????????????????????",null,[["Because explanation is polite and there are examples, it is easy to understand.",0,true,false]
,["Description is polite, and, there is a case, because the easy to understand.",0,true,false]
]
,[[0,24]
]
,"????????????????????????",0,0]
]
,1.0,null,[["ja"]
,null,[1.0]
,["ja"]
]
,null,null,null,null,null,null,null,null,null,[null,2]
]
推荐阅读
- java - 在 Spring Boot 中运行 Junit 测试用例时的初始化
- tensorflow - self_adjoint_eig 的问题
- google-cloud-firestore - 集合查询(单个)是否比 groupCollection 查询更快?
- c - 分配乘法表的问题
- javascript - Pixi.js - 窗口调整大小后重新定位元素
- azure-functions - 在 Azure Function 中添加自定义遥测属性
- javascript - nodemon 包未安装在 nodejs 中
- sql - 在单个 sqlQuery 中从 R 连接到多个 SQL 服务器数据库
- mysql - 获取按其“有很多”保险到期日中最早的订购日期订购的供应商列表
- php - Laravel 中的单元/功能测试在浏览器中返回 500 工作正常