简体   繁体   中英

Google translate via PHP Curl

I am trying to use Google URL to fetch the translation from Russian or Chinese or many other languages to English it doesn't work in CURL code and when I use the same URL in the postman app it works. I have tried many things also imported the code from postman to PHP file but still the same issue. also (utf-8 decode) works a bit but not completely.added 2 comments for 2 different languages try them you can see the error

<?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;
    }

The problem is, according to the error your code produces, down to the lack of SSL authentication. The error I observed in your code was:

cURL Error #:SSL certificate problem: unable to get local issuer certificate

Having seen your comment after penning initial answer the answer is much simpler - you need to urlencode the phrase you are trying to translate.

/* 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;
}   

Which yields:

[[["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]
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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