Page 1 of 1

Encode value to post to URL problem

Posted: Mon Jun 29, 2015 11:31 pm
by JosepM
Hi,

I ask for help. I'm blocked with this issue. Maybe it's a silly question but I don't how do it.

I need call this URL http://mymemory.translated.net/api/get? ... pair=en|it

Where "red" is the text to translate, in this case from English to Italian. Since Google Translator isn't free this service from mymemory is a cool alternative.

I put this into a PHP script that return the translated text directly from the JSON that the URL return.

http://31.193.137.141/trans2.php?value=zimmer
In this case is from Deutsch to Italian.

My problem is that if the vaule that is passed by POST to the URL don't have any accented char they work perfectly, but if have any accented char don't work. I know that is a issue with the encode way to send the text.

Code: Select all

put "http://api.mymemory.translated.net/get?q=" & urlencode(utf8encode(tItem)) & "&langpair=" & tOrigen & "|" & tDestino into tURL
put URL tURL into tResult
Calling inside Livecode this work perfectly, but using by POST don't.

Code: Select all

put "http://31.193.137.141/trans2.php" into tURL
put "value" & "=" & tfrase into myPostData
put myPostData & " --> " & urlencode(utf8encode(tfrase)) & cr after fld "f_log"
Post myPostData to URL tURL
put it into tResult
put utf8decode(tResult) & cr after fld "f_log"
put "----" & cr after fld "f_log"
put tItem & "->" & utf8decode(tResult) & cr after fld "f_translated"

PHP Script

Code: Select all

<?php
header('Content-type: text/plain; charset=utf-8');
error_reporting(E_ALL);

if (isset($_POST['value']))
{
	$valor=($_POST['value']);
}
else
{
	$valor=($_GET['value']);
}

echo "http://mymemory.translated.net/api/get?q=".$valor."&langpair=de|it"."\n\n";

// creating curl resource
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://mymemory.translated.net/api/get?q=".$valor."&langpair=de|it");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");

$output = curl_exec($ch);

curl_close($ch);      
$translate_it=json_decode($output,true);

$varuno = $translate_it['responseData']['translatedText'];
echo $varuno;
?>
Any idea what I'm doing wrong?

Salut,
Josep M

Re: Encode value to post to URL problem

Posted: Mon Jun 29, 2015 11:40 pm
by SparkOut
First glance, without having a chance to investigate properly, you are encoding tfrase for the log file, but not in the data you are posting. Is this intentional?

Re: Encode value to post to URL problem

Posted: Tue Jun 30, 2015 1:26 am
by JosepM
Hi,

Sorry. tfrase is encoded in the field. So they have the same enconding.

Salut,
Josep M

Re: Encode value to post to URL problem

Posted: Wed Jul 01, 2015 7:18 pm
by mwieder
Josep-

Why are you trying to POST data to the service?
I don't see anything in the api documentation about supporting the POST verb.

Unless you're trying to contribute a new translation pair to the service it looks like you should just use an http get call to retrieve information.

Re: Encode value to post to URL problem

Posted: Thu Jul 02, 2015 8:36 pm
by JosepM
Hi,

The call to the API is inside the PHP script, I pass by POST to the script to call the API, then extract from the JSON the tranlated text and return it.

Salut,
Josep M