Skip to content
Advertisement

LibreTranslate – different result with JS fetch and PHP curl

Hello I am trying to use this API with two different ways and I am not getting the same asnwer :

With Javascript :

const test = async () => {

    const res = await fetch("https://www.libretranslate.com/translate", {
        method: "POST",
        body: JSON.stringify({
            q: ["maison", "chat"],
            source: "fr",
            target: "es",
            format: "text",
            api_key: "XXXXXXXXXXX"
        }),
        headers: { "Content-Type": "application/json" }
    });
    console.log(await res.json());
}
test();

The result I get here is :

{ translatedText: [ 'casa', 'gato' ] }

Which is the expected result…

With PHP :

public function actionTest() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://www.libretranslate.com/translate");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt(
        $ch,
        CURLOPT_POSTFIELDS,
        http_build_query([
            'q' => json_encode(["maison", "chat"]),
            'source' => 'fr',
            'target' => 'es',
            'format' => 'html',
            'api_key' => 'XXXXXXXXXXX'
        ])
    );

    // curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    //     'Content-Type: application/json'
    // ));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = json_decode(curl_exec($ch));
    print_r($server_output);
    curl_close($ch);
}

The result I get here is :

stdClass Object
(
    [translatedText] => ["maison", "chat"]
)

Which is not the expected one..

This problem happens in PHP when I try to translate multiple words, if I only translate one word (‘chat’ for example) here is what I get :

stdClass Object
(
    [translatedText] => gato
)

Also as you can see in the PHP function, the code where I try to set headers is commented because when I uncomment it this is what I get :

stdClass Object
(
    [error] => The browser (or proxy) sent a request that this server could not understand.
)

Thanks !

Advertisement

Answer

I fixed this by uting json_encode on the whole data instead of http_build_query, I also uncommented the header settings here is my working code :

public function actionTest() {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://www.libretranslate.com/translate");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt(
        $ch,
        CURLOPT_POSTFIELDS,
        json_encode([
            'q' => ['maison', 'chat'],
            'source' => 'fr',
            'target' => 'es',
            'format' => 'html',
            'api_key' => 'XXXXXXXXXXX'
        ])
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json'
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = json_decode(curl_exec($ch));
    print_r($server_output);
    curl_close($ch);
}

and the response :

(
    [translatedText] => Array
        (
            [0] => casa
            [1] => gato
        )

)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement