谷歌翻译通过PHP Curl



我正在尝试使用谷歌网址获取从俄语或中文或许多其他语言到英语的翻译,它在 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]
]

最新更新