繁体   English   中英

谷歌通过 PHP Curl 翻译

[英]Google translate via 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]
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM