Google translate, как да реша проблема с charset ?

C++ JAVA
Post Reply
User avatar
Ticketa
Турист
Турист
Posts: 594
Joined: Mon Feb 27, 2012 1:54 pm
Answers: 24
Location: in /root
Contact:

Google translate, как да реша проблема с charset ?

Post by Ticketa » Thu Dec 24, 2020 2:04 pm

Привет,
Опитвам се да извлека "превод" от Гугъл преводача чрез curl , обаче срещам проблем с кирилицата. Вероятно греша при изпращането или обработката на резултата.

Това е отговора:
[[["?????????","hello",null,null,1]
,[null,null,"Zdravei?te","h??l?"]
]
,[["?????????????",["???????????","??????? ?? ????????"]
,[["???????????",["hello"]
,null,3.054422E-4]
,["??????? ?? ????????",["hello"]
,null,1.5358598E-4]
]
,"hello",1]
,["??????",["????? '???'"]
,[["????? '???'",["hello"]
]
]
,"hello",2]
]
,"en",null,null,[["hello",null,[["?????????",1000,true,false]
,["???????",1000,true,false]
,["???????",1000,true,false]
]
,[[0,5]
]
,"hello",0,0]
]
,1.0,[]
,[["en"]
,null,[1.0]
,["en"]
]
,null,null,null,[["???????????",[["used as a greeting or to begin a phone conversation.","m_en_gbus0460730.012","hello there, Katie!"]
]
,"hello"]
,["?????????????",[["an utterance of ?hello?; a greeting.","m_en_gbus0460730.025","she was getting polite nods and hellos from people"]
]
,"hello"]
,["??????",[["say or shout ?hello?; greet someone.","m_en_gbus0460730.034","I pressed the phone button and helloed"]
]
,"hello"]
]
,[[["\u003cb\u003ehello\u003c/b\u003e, what's this?",null,null,null,3,"neid_9510"]
,["I said \u003cb\u003ehello\u003c/b\u003e to him",null,null,null,3,"neid_9507"]
,["she refused and, \u003cb\u003ehello\u003c/b\u003e, I'm her manager!",null,null,null,3,"neid_9509"]
,["\u003cb\u003ehello\u003c/b\u003e, is anybody in?",null,null,null,3,"neid_9508"]
,["\u003cb\u003ehello\u003c/b\u003e there, Katie!",null,null,null,3,"m_en_gbus0460730.012"]
]
]
,null,null,null,null,[null,2]
]
Това е кода:

Code: Select all

<?php
    header('Content-Type: application/json; charset=utf-8');

    $dtext = "hello";
    // the next comment text work in $dtext 
    
    $text = str_replace(" ","%20",$dtext);
/* 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=bg&hl=bg&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=&ie=UTF-8&oe=UTF-8&&q=".urlencode( $dtext );

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $host,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "UTF-8",
    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 {
    print_r($response);
}
Първия проблем: не успявам да оправя charset

Втория проблем: не успявам да взема превода като стринг

uphero
Потребител
Потребител
Posts: 781
Joined: Mon Feb 20, 2012 12:26 pm
Answers: 34
Location: Казанлък
Contact:

Re: Google translate, как да реша проблема с charset ?

Post by uphero » Sat Dec 26, 2020 7:13 am

Първия гугъл резултат и:

Code: Select all

function file_get_contents_curl($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
$dtext = 'hello';
$json = file_get_contents_curl("http://translate.google.com/translate_a/single?client=webapp&sl=auto&tl=bg&hl=bg&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=&ie=UTF-8&oe=UTF-8&&q=".urlencode($dtext));
$json = json_decode($json);
echo "<pre>";
var_dump($json);
Кирилицата е ОК но като го гледам тоя линк и ми става лошо, ти документацията чете ли я? Аз такова нещо там не видях.

Post Reply