簡體   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