Encode value to post to URL problem

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
JosepM
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 344
Joined: Tue Jul 20, 2010 12:40 pm

Encode value to post to URL problem

Post by JosepM » Mon Jun 29, 2015 11:31 pm

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

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Encode value to post to URL problem

Post by SparkOut » Mon Jun 29, 2015 11:40 pm

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?

JosepM
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 344
Joined: Tue Jul 20, 2010 12:40 pm

Re: Encode value to post to URL problem

Post by JosepM » Tue Jun 30, 2015 1:26 am

Hi,

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

Salut,
Josep M

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Encode value to post to URL problem

Post by mwieder » Wed Jul 01, 2015 7:18 pm

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.

JosepM
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 344
Joined: Tue Jul 20, 2010 12:40 pm

Re: Encode value to post to URL problem

Post by JosepM » Thu Jul 02, 2015 8:36 pm

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

Post Reply

Return to “Talking LiveCode”