0

I was trying to read Google Translate data from Linux terminal cURL:

curl -i --user-agent "" -d "sl=en" -d "tl=sk" --data-urlencode "text=hi" https://translate.google.com

It returned whole page, where I could see Translate result. Result was in one HTML element. But when I tried it with PHP's cURL, it doesn't outputed the same HTML and I wasn't able to find Translate result.

$data = [
    "sl" => "en",
    "tl" => "sk",
    "text" => urlencode("hi");
];

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL, "https://translate.google.com");
curl_setopt($ch,CURLOPT_POST, count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_HEADER, true);
curl_setopt($ch,CURLOPT_USERAGENT, "");


curl_exec($ch);

Thank you to help me, how can I return in PHP the same result as in Linux cURL.

2 Answers 2

1

Use $response = curl_exec($ch);

        $data = [
            "sl" => "en",
            "tl" => "sk",
            "text" => urlencode("hi")
        ];

        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL, "https://translate.google.com");
        curl_setopt($ch,CURLOPT_POST, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch,CURLOPT_HEADER, true);
        curl_setopt($ch,CURLOPT_USERAGENT, "");


        $respond = curl_exec($ch);

        echo $respond;

        curl_close($ch);
Sign up to request clarification or add additional context in comments.

2 Comments

No. The problem is, that first returns different HTML then second. In first is Translate result, but in second isn't.
Isn't problem in Cookies?
0

If you are just looking for the translate result, all you need to do is use the google translate API and it is very easy to use this API.

http://translate.google.com/translate_a/t?client=t&text=Hello&hl=en&sl=en&tl=sk&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1

This API will return you a txt file which you can read in PHP and you will be able to get the translated result from there.

If you look at the source code of translate.google.com then you can find this there:

f.src=c;

That c is a source of javascript. Now that c is hidden in that source code which is very hard to understand and could possibly take a lot of time. The data returned on google.translate.com is obviously coming through an Ajax request, this request might be in some other javascript file which is not viewable directly, it could f.src=c; or it could be something else. So there is no direct result or translation on the page translate.google.com, therefore PHP's curl won't get you the translation from that page because translation on that page is coming through an ajax request from somewhere else (and that somewhere else is the google API I mentioned above), there is no actual translation on that page which you can read directly as a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.